How to convert XML to JSON?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 04:52 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 04:54 AM
Hi,
check this
Converting XML to JSON failing with gs.xmlToJSON when XML contains #&13;
gs.xmlToJSON sets NULL to true
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 05:19 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 08:28 PM
Any suggestions on this would be helpful.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2021 01:17 AM
Any advice on this would be helpful.
Note: I am using scoped Application
Thank you.