Remove Element in JSON after conversion

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!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Jan 21, 2017 at 07:13 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 23, 2017 at 11:09 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 25, 2017 at 09:45 AM

    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
    10|10000 characters needed characters exceeded

    • Former Member

      Hi everyone thanks for answers,

      i found out that it can be done in standard conversion step.

      You need to point elements to be an array under Convert XML Element to JSON Array

Skip to Content