When converting the JSON data to XML using standard JSON to XML converter, the Root node and the other sub node will not be added, if there are multiple messages in the payload. This will be an issue while sending converted XML to any HTTP receivers.
So use the below script to make this work before the JSON to XML convertor
Groovy Script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {
def jsonOP = message.getBody(String.class);
jsonOP=jsonOP.toString()
def json_to_str=jsonOP.substring(1, jsonOP.length()- 1);
json_to_str="{\"Root\": [{\"EmpRoot\":["+json_to_str+"]}]}"
message.setBody(json_to_str);
return message;
}
Input:
[
{
"EmpID": 5656,
"EmpName": "Ram Kumar",
"Grade": 5
},
{
"EmpID": 5659,
"EmpName": "Ram Kumar",
"Grade": 2
}
]
Output:
<?xml version="1.0" encoding="UTF-8" ?>
<Root>
<EmpRoot>
<EmpID>5656</EmpID>
<EmpName>Ram Kumar</EmpName>
<Grade>5</Grade>
</EmpRoot>
<EmpRoot>
<EmpID>5659</EmpID>
<EmpName>Ram Kumar</EmpName>
<Grade>2</Grade>
</EmpRoot>
<Root>
So use the below script to make this work before the JSON to XML convertor
Groovy Script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {
def jsonOP = message.getBody(String.class);
jsonOP=jsonOP.toString()
def json_to_str=jsonOP.substring(1, jsonOP.length()- 1);
json_to_str="{\"Root\": [{\"EmpRoot\":["+json_to_str+"]}]}"
message.setBody(json_to_str);
return message;
}
Input:
[
{
"EmpID": 5656,
"EmpName": "Ram Kumar",
"Grade": 5
},
{
"EmpID": 5659,
"EmpName": "Ram Kumar",
"Grade": 2
}
]
Output:
<?xml version="1.0" encoding="UTF-8" ?>
<Root>
<EmpRoot>
<EmpID>5656</EmpID>
<EmpName>Ram Kumar</EmpName>
<Grade>5</Grade>
</EmpRoot>
<EmpRoot>
<EmpID>5659</EmpID>
<EmpName>Ram Kumar</EmpName>
<Grade>2</Grade>
</EmpRoot>
<Root>
No comments:
Post a Comment