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.

Convert xml to json in washington version

mike56
Tera Expert

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.

 

1 ACCEPTED SOLUTION

mike56
Tera Expert

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);

 

 

 

 

View solution in original post

3 REPLIES 3

Juhi Poddar
Kilo Patron
Kilo Patron

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

Community Alums
Not applicable

@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

mike56
Tera Expert

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);