Question
Rabobank
NL
Last activity: 23 Aug 2022 3:48 EDT
Convert a JSON string to XML string
we have a requirement to transmit data from one system to another, basically it will be a complex JSON provided to us and then we should be able to forward it either in JSON string or XML.string
Now to achieve JSON to XML there are api available for ClipboardPage class in java like adoptJSONObject, getXML. but the problem is the XML generated is not desirable as it can contain pega naming convention for lists like pagelist, repeating index = 1.
How can I create a standard XML, which can be accepted outside by a non-pega application?
I am attaching an example for instance: file "whatIGet" is constructed using pega api and file "whatToAchieve" is what I want to get.
Also I wrote below function to convert JSON to XML.
PublicAPI tools = (PegaAPI) ThreadContainer.get().getPublicAPI();
PRThread thread = tools.getThread();
String retValue = "";
if(jsonString.length()>5){
try{
ClipboardPage responseDataPg = tools.createPage("$ANY", "JsonPageConvXML");
responseDataPg.adoptJSONObject(jsonString);
retValue = responseDataPg.getXML();
thread.removePage(responseDataPg);
} catch(Exception e) {
throw new PRRuntimeException(e);
}
}
return retValue;
@Gaurav_Kumar Perhaps a better solution would be to use the JSONObject class.The below code snippet should be able to convert a JSON string directly into an XML string, without the additional PEGA-related fields attached to it.
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
Better still, this class is already present within your PEGA instance, so no additional 3rd party JAR is required to be installed.