on 01-20-2017 3:52 PM
Hi Experts,
I have the following XML:
<?xml version="1.0" encoding="UTF-8" ?> <root> <element> <name>Donald</name> <gender>M</gender> <country>US</country> <id>1</id> </element> <element> <name>Hilary</name> <gender>F</gender> <country>FR</country> <id>2</id> </element> <element> <name>Obama</name> <gender>M</gender> <country>JP</country> <id>3</id> </element> </root>
After XML-JSON Converter, it will become:
{"element": [{"name":"Donald","gender":"M","country":"US","id":"1"}, {"name":"Hilary","gender":"F","country":"FR","id":"2"}, {"name":"Obama","gender":"M","country":"JP","id":"3"}] }
Problem is, my desired JSON message is
[{"name":"Donald","gender":"M","country":"US","id":"1"}, {"name":"Hilary","gender":"F","country":"FR","id":"2"}, {"name":"Obama","gender":"M","country":"JP","id":"3"} ]
Obviously, I don't want the "element" in my JSON message. So my question is, is it possible to omit this "element"?
Thanks in advance!
Hi Mark
There's only so much you can do in the XML to JSON converter, so if you need to craft more specific JSON output, you need to do so in code. Try adding the following Groovy script after the converter step:
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String
def jsonParser = new JsonSlurper()
def jsonObject = jsonParser.parseText(body)
message.setBody(JsonOutput.toJson(jsonObject["element"]))
return message;
}
This assumes you've checked "Suppress JSON Root Element".
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Morten! Was also able to do it via JavaScript
importClass(com.sap.gateway.ip.core.customdev.util.Message);
importClass(java.util.HashMap);
function processData(message) {
var data = JSON.parse(message.getBody(java.lang.String));
var request = JSON.stringify(data.element);
message.setBody(request);
return message;
}
hi guys,
do you know how to convert xml into json array?
I need - "specializations": [{ "id": "12.180" }, { "id": "24.493" }],
if i use xsd schema
<xs:element name="specializations" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:float" minOccurs="0" maxOccurs="unbounded" name="id"/>
</xs:sequence>
</xs:complexType>
</xs:element>
i only get "specializations":{"id":["12.180","24.493"]}, that's not execlty what reciever wants.
Hi Morten,
When i am using this groovy script its working fine but all fields are shuffling.
import com.sap.gateway.ip.core.customdev.util.Message;import groovy.json.JsonSlurper
import groovy.json.JsonOutput
def Message processData(Messagemessage){
def body =message.getBody(java.lang.String)asString
def jsonParser =new JsonSlurper()
def jsonObject = jsonParser.parseText(body)message.setBody(JsonOutput.toJson(jsonObject["MT_Request"]))returnmessage;}
Is there any other script.
Please find the below link.
https://answers.sap.com/questions/364055/how-to-remove-root-element-in-json-after-conversio.html
Thanks and Regards,
Md.Thouheed
Hi Alexey,
I believe the closest XSD that you can use is this
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="specializations">
<xs:complexType>
<xs:sequence>
<xs:element name="element" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:float"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Then use XML to JSON converter with Suppress JSON Root element enabled. The result will be:
{"specializations":{"element":[{"id":"12.180"},{"id":"24.493"}]}}
Then one option is to add a script like below to get rid of "element' and have your desired output
function processData(message) {
var new_array = {
specializations: []
}
var data = JSON.parse(message.getBody(java.lang.String));
new_array.specializations = data.specializations.element;
message.setBody(JSON.stringify(new_array));
return message;
}
Hope this helps.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
successive elements with the same name are always transformed to an array in the JSON converter, therefore you get an array value for the id element.
This is also described in the documentation of the JSON converter.
Best regards,
Mandy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
10 | |
9 | |
9 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.