Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
rasjoshi
Active Contributor
12,829
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.shivarambhat 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/3a02829ea84e4f7dac62bdd10d0...

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

 

Thanks & Regards,

Rashmi Joshi
2 Comments
Labels in this area