- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2024 09:59 AM - edited 12-26-2024 09:00 AM
Hi,
I am working on a soap integration and the information is retrieved in xml format. I need to convert it to json in order to use the values that are being returned. I am thanking in advance for any help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 08:56 AM - edited 12-26-2024 08:58 AM
In the end I have not converted the xml to json because none of the solutions worked.
I read the xml parameters as following:
var gr = new GlideRecord("incident");
gr.get("749dbebd05619ad44d97162086ffc6b0");
gs.log(gr.description);//this is the xml
var xmlDoc=new XMLDocument2();
xmlDoc.parseXML(gr.description);//this is the xml
var ActNumNew=xmlDoc.getNodeText("//ActNumNew");
gs.log(ActNumNew);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2024 10:12 AM - edited 11-09-2024 10:25 AM
Hello @mike56
To convert XML payload to JSON object use OOTB method
Method1: toObject() method returns the XML elements as JSON properties
Refer the Servicenow doc to know more.
Method2:
var jsonObj = gs.xmlToJSON(res_body); // Converting XML into a JSON object
To know more about this, refer to the knowledge article provided by ServiceNow support.
"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2024 10:29 AM
@mike56 You can try below code to change XML data into JSON if works for you:
// Assuming 'responseXML' is the XML response string from SOAP
var xmlDoc = GlideXMLUtil.parse(responseXML);
// Convert the XML document to a JSON-like JavaScript object
var jsonResult = GlideXMLUtil.toObject(xmlDoc);
// Log the JSON result (for debugging)
gs.info(JSON.stringify(jsonResult));
// Now you can use 'jsonResult' as a JSON object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 08:56 AM - edited 12-26-2024 08:58 AM
In the end I have not converted the xml to json because none of the solutions worked.
I read the xml parameters as following:
var gr = new GlideRecord("incident");
gr.get("749dbebd05619ad44d97162086ffc6b0");
gs.log(gr.description);//this is the xml
var xmlDoc=new XMLDocument2();
xmlDoc.parseXML(gr.description);//this is the xml
var ActNumNew=xmlDoc.getNodeText("//ActNumNew");
gs.log(ActNumNew);