The CreatorCon Call for Content is officially open! Get started here.

How to convert XML to JSON?

Ranjit Singh R1
Kilo Expert

I have made a SOAP call and I am getting a response. I have a parameter in the Response which is base64 encoded. So I have parsed that parameter and decoded it. Now the decoded value looks familier however I have to convert the decoded string into JSON so that I can easily dot walk to the objects and fetch those values. Here is the script for that: 

    var responseBodyRetrieveReport = responseRetrieveReport.getBody();
    var statusRetrieveReport = responseRetrieveReport.getStatusCode();
    gs.info('statusRetrieveReport:' + statusRetrieveReport);
    gs.info('statusRetrieveReport:' + responseBodyRetrieveReport);
    var xmlDocTokenRetrieveReport = new XMLDocument2();
    xmlDocTokenRetrieveReport.parseXML(responseBodyRetrieveReport);
    var tokenRetrieveReport = xmlDocTokenRetrieveReport.getNode("//ReportStream").getTextContent();
    gs.info('statusRR:' + tokenRetrieveReport);
    var decodedString = gs.base64Decode(tokenRetrieveReport);
	gs.info('statusRRR:' + decodedString);

I am getting a log in the variable decodedString and I have to convert that XML body to JSON. Please suggest.

Note: I am using scoped Application

Thank you.

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

check this

Converting XML to JSON failing with gs.xmlToJSON when XML contains #&13;

gs.xmlToJSON sets NULL to true

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I am trying this however it returns null. I imported the script include and scheduled job and tried to replicate the same however I am getting null as response:

 var decodedString = gs.base64Decode(tokenRetrieveReport);
	gs.info('statusRRR:' + decodedString);
	var okHelper = new  global.OkXMLHelper();
var okObj = okHelper.toObject(decodedString);
	//var okStr = JSON.stringify(okObj, null, 2);
         var okStr = JSON.stringify(okObj);
	gs.info('statusRRRR:'+okStr);

find_real_file.png

Ranjit Singh R1
Kilo Expert

Any suggestions on this would be helpful.

 

Thank you.

Ranjit Singh R1
Kilo Expert

Any advice on this would be helpful. 

Note: I am using scoped Application

Thank you.