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
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
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.
Add comment