Iterating through XML response to create records on another table

Peter Reynolds1
Tera Expert

I have an upcoming requirement to gather integrate ith another system and the response will be in XML.

I need to iterate through the various elements and create records and populate values another table.

I have been looking at converting the XML to a Json Object, to no avail. I have also looked at many pages using the various "child node iterators" but again, I can seem to get t all working.

Using the Rest API explorer I am simply passing 

GET https://devXXXXXX.service-now.com/api/now/table/incident?sysparm_query=active%3Dtrue&sysparm_fields=...

 and my response

 

<?xml version="1.0" encoding="UTF-8"?><response><result><number>INC0010021</number><short_description>testpr</short_description></result><result><number>INC0010040</number><short_description>testprxml</short_description></result><result><number>INC0010025</number><short_description>testpr</short_description></result></response>

 

My intention is to create another record on another table and use the number and short description fields as values on that other table. 

Thank you all 

1 ACCEPTED SOLUTION

Ajay61
Kilo Sage

Hi Peter,
Please find the below articles to convert the XML response to JSON.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0957116

OR

Use below code in BR

var res_body = response.getBody();

var jsonObj = gs.xmlToJSON(res_body); // Converting XML into a JSON object

var results =  jsonObj.response.result;
for (var i = 0; i < results.length; i++) {
    var incidentNumber = results[i].number;
    gs.info('Incident Number: ' + incidentNumber);
}

View solution in original post

4 REPLIES 4

SumanKumarM
Tera Contributor

Hello Peter,

Please refer below article which has information on how to convert XML to JSON Payload

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0957116

 

Please mark helpful and correct, if it resolves your issue.

 

BR,

Suman.

SumanKumarM
Tera Contributor

Hello Peter,

Please use below code snippet, if you are not able to resolve the issue with my previous reference.

var request = new sn_ws.RESTMessageV2();
    request.setHttpMethod('GET');
    var user = 'username';
    var password = 'password';
    request.setBasicAuth(user, password);
    request.setRequestHeader("Accept", "application/xml");
    var response = request.execute();
    var res_body = response.getBody();// XML Response
    //gs.info('XML Response:\n\n' + response.getBody());
    var jsonObj = gs.xmlToJSON(res_body); // Converting XML into a JSON object
    var parseJsonStr = JSON.stringify(jsonObj);
    var parseJsonObj = JSON.parse(parseJsonStr);
    //gs.info('JSON Object: ' + parseJson);
    gs.info('Number: ' + parseJsonObj.response.result.number);
    //var str = JSON.stringify(jsonObject); // Converting JSON object into a String
    //gs.info('JSON String: ' +str);
 
Please mark helpful and correct, if it resolves your issue.
 
BR,
Suman.

Ajay61
Kilo Sage

Hi Peter,
Please find the below articles to convert the XML response to JSON.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0957116

OR

Use below code in BR

var res_body = response.getBody();

var jsonObj = gs.xmlToJSON(res_body); // Converting XML into a JSON object

var results =  jsonObj.response.result;
for (var i = 0; i < results.length; i++) {
    var incidentNumber = results[i].number;
    gs.info('Incident Number: ' + incidentNumber);
}

Thank you for this. I had seen the snippets from other posts but I had so many variations I simply took out the var results =  jsonObj.response.result; and couldnt see it. Appreciate your time.