Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi,

is the OkXMLHelper a new script include in global scope?

you can also create script include in global scope and pass the xml and get json using gs.xmlToJSON()

Regards
Ankur

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