Skip to Content
Technical Articles

SAP CPI JSON to XML Limitations

Dear SAPiens,

There are some limitations while using JSON to XML Conversion in built function. One of them is –>

“The root JSON object must contain exactly one member. If the root JSON object contains more than one member (for example, {“a”:”avalue”,”b”:”bvalue”}), a javax.xml.stream.XMLStreamException is thrown. If the root JSON object is empty ({}), a javax.xml.stream.XMLStreamException is thrown. As of cluster version 1.21, a JsonXmlException or IllegalStateException (for {}) is thrown.”

–> For this limitation apply point no.2 directly

“JSON texts that start with an array (for example, [{“a”:{“b”:”bvalue”}}] ) are not supported. The JSON text must start with a JSON object such as {{“a”:{“b”:”bvalue”}}}. A javax.xml.stream.XMLStreamException is thrown. As of cluster version 1.21, a JsonXmlException is thrown.”

–> Follow below steps

I have gone through many threads on SCN and got pretty good understanding about the issue and how to fix this. Thanks to Sriprasad Shivaram Bhat for his valuable inputs on CPI thread. Below I am sharing my study Iflow which is handling such scenarios.  P.S. – This is to explore the issue and not a real-time case.

Below is the brief of different steps from the IFlow: –

  1. Content modifier(A) to get the input JSON file which is starting with [ ]
  2. Content Modifier(B) to add root and elements in the JSON payload (Another way is to write script)
  3. JSON to XML Converter in built function
  4. Content modifier(C) to save XML file in Body
  5. XML to JSON Converter in built function
    1. Only Suppress JSON root element available
    2. Element will be added in the payload
    3. All fields will be considered as String
  6. Groovy Script(1) to remove element from JSON file and convert string into integer
  7. Content modifier(D) to save JSON file

________________________________________________________________________________

1. Content Modifier(A): – Input JSON File

[
{“id”: 1},
{“id”: “OpenNew”, “label”: “Open New”},
null,
{“id”: “ZoomIn”, “label”: “Zoom In”},
{“id”: “ZoomOut”, “label”: “Zoom Out”},
{“id”: “OriginalView”, “label”: “Original View”},
null,
{“id”: 10000},
{“id”: “Pause”},
{“id”: “Mute”},
null,
{“id”: “Find”, “label”: “Find…”},
{“id”: “FindAgain”, “label”: “Find Again”},
{“id”: “Copy”},
{“id”: “CopyAgain”, “label”: “Copy Again”},
{“id”: “CopySVG”, “label”: “Copy SVG”},
{“id”: “ViewSVG”, “label”: “View SVG”},
{“id”: “ViewSource”, “label”: “View Source”},
{“id”: “SaveAs”, “label”: “Save As”},
null,
{“id”: “Help”},
{“id”: “About”, “label”: “About Adobe CVG Viewer…”}
]

 

2. Content Modifier(B): –

 

3. JSON to XML Converter

4. Content Modifier(C)

This step is just to hold and check payload in XML format

5. XML to JSON Converter: –

6. Groovy Script(1): – This code will help to remove element from the JSON payload and convert string      to Integer

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) 
{    
    def JSON = message.getBody(String)
    def JSON_Rem = JSON.substring(11,JSON.length()-1)
    message.setBody(JSON_Rem)
    String output = JSON_Rem.replaceAll("\"(\\d+)\"", "\$1");
    message.setBody(output)
       return message;
}

7. Content modifier(D) to save JSON file

References: –

https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/3a02829ea84e4f7dac62bdd10d06b084.html

2521044 – CPI: XML to JSON Converter treats Integer as String

 

Thanks & Regards,

Rashmi Joshi

2 Comments
You must be Logged on to comment or reply to a post.
  • Hi Rashmi

    Modifying the JSON payload I would always do it with a Groovy Script. Groovy offers powerful means for handling of JSON data. The whole functionality could be rewritten in a single script (without any converter adapters):

    import com.sap.gateway.ip.core.customdev.util.Message;
    import groovy.json.*
    
    def Message processData(Message message)
    {
        //parse Json
        def payload = message.getBody(Reader)
        def js = new JsonSlurper()
        def JsonObj = js.parse(payload)
    
        //Example: removal of null items
        JsonObj.removeAll { it == null }
    
        //Example: JSON manipulation
        def item = JsonObj.find {it.id == 'ViewSVG'}
        if (item != null) {
            item.label = 'Label Changed'
        }
    
        //convert collections/maps back to JSON string
        def payloadMod = new JsonBuilder(JsonObj).toString()
        
        message.setBody(payloadMod)
        return message
    }

    The result will be:

    [{
            "id": 1
        }, {
            "id": "OpenNew",
            "label": "Open New"
        }, {
            "id": "ZoomIn",
            "label": "Zoom In"
        }, {
            "id": "ZoomOut",
            "label": "Zoom Out"
        }, {
            "id": "OriginalView",
            "label": "Original View"
        }, {
            "id": 10000
        }, {
            "id": "Pause"
        }, {
            "id": "Mute"
        }, {
            "id": "Find",
            "label": "Find\u2026"
        }, {
            "id": "FindAgain",
            "label": "Find Again"
        }, {
            "id": "Copy"
        }, {
            "id": "CopyAgain",
            "label": "Copy Again"
        }, {
            "id": "CopySVG",
            "label": "Copy SVG"
        }, {
            "id": "ViewSVG",
            "label": "Label Changed"
        }, {
            "id": "ViewSource",
            "label": "View Source"
        }, {
            "id": "SaveAs",
            "label": "Save As"
        }, {
            "id": "Help"
        }, {
            "id": "About",
            "label": "About Adobe CVG Viewer\u2026"
        }
    ]
    • Hi Thomas Buerki,

      Thanks for sharing the code. However, the document was published to share details in chunk.

      For e.g. some might be having multiple elements which is why conversion giving error so such small changes can be achieved without groovy post which built in function can be used.

      I believe many consultants are still in learning phase of groovy. It will be great if you can share some useful links apart from groovy official document link.

      TIA,

      Rashmi