How to parse ECC payload xml to pull a specific value into an array

Aditya Banka2
Tera Guru

We have a requirement where we need to pull the ECC payload using a scheduled job to check for authentication issues using specific keywords and create an incident with a specific queue.

 

I was able to achieve it, but the tricky part for me is to parse the xml to pull the failed RITM numbers from the ECC payload and push them into a array and display them in the description of the incident.

 

Below is the sample ECC payload. I want to pull the highlighted RITM number from the xml and push it into a array. 

=======================================

<?xml version="1.0" encoding="UTF-8"?>
<results error="Method failed: (/api/v2/job_templates/300000/launch/) with code: 401 - Invalid username/password combo" probe_time="656" result_code="900000">
<result error="Method failed: (/api/v2/job_templates/300000/launch/) with code: 401 - Invalid username/password combo">
<output>{"detail":"Authentication credentials were not provided. To establish a login session, visit /api/login/."}</output>
</result>
<httpHeaders>
<httpHeader name="Server" value="nginx"/>
<httpHeader name="Date" value="Wed, 19 Apr 2023 11:08:17 GMT"/>
<httpHeader name="Content-Type" value="application/json"/>
<httpHeader name="Content-Length" value="107"/>
<httpHeader name="Connection" value="keep-alive"/>
<httpHeader name="WWW-Authenticate" value="Bearer realm=api authorization_url=/api/o/authorize/"/>
<httpHeader name="Vary" value="Accept, Accept-Language, Origin, Cookie"/>
<httpHeader name="Allow" value="GET, POST, HEAD, OPTIONS"/>
<httpHeader name="X-API-Product-Version" value="3.8.3"/>
<httpHeader name="X-API-Product-Name" value="Red Hat Ansible Tower"/>
<httpHeader name="X-API-Node" value="usdcaqlapp00000002.test.net"/>
<httpHeader name="X-API-Time" value="0.233s"/>
<httpHeader name="Content-Language" value="en"/>
<httpHeader name="X-API-Total-Time" value="0.581s"/>
</httpHeaders>
<parameters>
<parameter name="agent" value="mid.server.test_Dev_ldap_Integration"/>
<parameter name="signature" value=""/>
<parameter name="rest_password" value="SNC_ENC_VAL[iGCsS7kQulbXM7KgEqdOZl+H80O+STTrd8DLYNRQ4glF]"/>
<parameter name="source" value="https://ansible-qa.test.net/api/v2/job_templates/300000/launch/"/>
<parameter name="skip_sensor" value="true"/>
<parameter name="content" value="{&quot;extra_vars&quot;: &#13;&#10;{&quot;FIRST&quot;: &quot;&quot;,&quot;LAST&quot;:&quot;&quot;,&quot;ALIAS&quot;:&quot;testmailbox5&quot;,&quot;DISPLAY&quot;:&quot;testmailbox5&quot;,&quot;NAME&quot;:&quot;testmailbox5&quot;,&quot;OWNER&quot;:&quot;test2@te.com&quot;,&quot;BACKUPOWNER&quot;:&quot;test1@te.com&quot;,&quot;RITM_NUMBER&quot;:&quot;RITM0000001&quot;,&quot;EDIT&quot;:&quot;N&quot;,&quot;REMOVE&quot;:&quot;N&quot;}, &quot;inventory&quot;:2, &quot;credentials&quot;: [35,47]}"/>
<parameter name="message_headers" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;fields&gt;&lt;field name=&quot;Accept&quot; value=&quot;application/json&quot;/&gt;&lt;field name=&quot;Content-Type&quot; value=&quot;application/json&quot;/&gt;&lt;/fields&gt;"/>
<parameter name="sys_id" value="4cf4926d1bde6d502ca92022604bcbb5"/>
<parameter name="caller_scope" value="global"/>
<parameter name="originating_scope" value="global"/>
<parameter name="http_method" value="POST"/>
<parameter name="from_host" value=""/>
<parameter name="follow_redirect" value="true"/>
<parameter name="source_record" value="f6f89c9287cdad989f2cca2acebb350b"/>
<parameter name="sys_created_on" value="2023-04-19 11:08:16"/>
<parameter name="sys_domain" value="global"/>
<parameter name="transaction_name" value="Workflow5fc49ea51bdead503ebbca20604bcba9 - system"/>
<parameter name="state" value="ready"/>
<parameter name="message_parameters" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;fields/&gt;"/>
<parameter name="mid_server" value="test_Dev_ldap_Integration"/>
<parameter name="http_status_code" value="401"/>
<parameter name="response_to" value=""/>
<parameter name="http_timeout" value="30000"/>
<parameter name="from_sys_id" value=""/>
<parameter name="session_id" value="glide.scheduler.worker.5"/>
<parameter name="priority" value="1"/>
<parameter name="agent_correlator" value=""/>
<parameter name="processed" value=""/>
<parameter name="error_string" value=""/>
<parameter name="sequence" value="187993369ed0000001"/>
<parameter name="start_time" value="1681902496236"/>
<parameter name="mid_instance_url" value="https://test.service-now.com/"/>
<parameter name="rest_user" value="te0s1170"/>
<parameter name="name" value="post"/>
<parameter name="artifact_scope_id" value="global"/>
<parameter name="topic" value="RESTProbe"/>
<parameter name="app_scope" value="global"/>
<parameter name="source_table" value="wf_activity"/>
<parameter name="user" value="system"/>
<parameter name="queue" value="output"/>
<parameter name="ecc_queue" value="4cf4926d1bde6d502ca92022604bcbb5"/>
</parameters>
</results>

 

Any ideas on how to do this?

1 ACCEPTED SOLUTION

Aditya Banka2
Tera Guru

Hello Sandeep,

 

Thank you for providing your inputs. It's partially helpful to me. 

 

Below is the code I have used to parse the xml using getAttribute("//parameter[@name=\"content\"]", "value") and the value for this attribute is a JSON object. So, I have used JSON.parse to fetch the RITM number from it.

 

 

var arr = [];
var ecc_query = new GlideRecord("ecc_queue");

ecc_query.addEncodedQuery('sourceLIKEhttps://ansible^sys_created_onONYesterday@javascript&colon;gs.beginningOfYesterday()@javascript&colon;gs.endOfYesterday()^payloadLIKE{"detail":"Authentication credentials were not provided. To establish a login session, visit /api/login/."}^name=post');
ecc_query.query();
while (ecc_query.next()) {
var xmlString = ecc_query.payload;
var xmlDoc = new XMLDocument(xmlString, true);
var fetchRITM = xmlDoc.getAttribute("//parameter[@name=\"content\"]", "value");
var responseObj = JSON.parse(fetchRITM);
var finalRITM = responseObj.extra_vars.RITM_NUMBER;
arr.push(finalRITM.toString());

}

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@Aditya Banka2 Please refer to this thread https://www.servicenow.com/community/developer-forum/parsing-through-an-xml-string/m-p/1960817 here author has parsed the XML using the XMLHelper script include.

 

Hope it helps.

Aditya Banka2
Tera Guru

Hello Sandeep,

 

Thank you for providing your inputs. It's partially helpful to me. 

 

Below is the code I have used to parse the xml using getAttribute("//parameter[@name=\"content\"]", "value") and the value for this attribute is a JSON object. So, I have used JSON.parse to fetch the RITM number from it.

 

 

var arr = [];
var ecc_query = new GlideRecord("ecc_queue");

ecc_query.addEncodedQuery('sourceLIKEhttps://ansible^sys_created_onONYesterday@javascript&colon;gs.beginningOfYesterday()@javascript&colon;gs.endOfYesterday()^payloadLIKE{"detail":"Authentication credentials were not provided. To establish a login session, visit /api/login/."}^name=post');
ecc_query.query();
while (ecc_query.next()) {
var xmlString = ecc_query.payload;
var xmlDoc = new XMLDocument(xmlString, true);
var fetchRITM = xmlDoc.getAttribute("//parameter[@name=\"content\"]", "value");
var responseObj = JSON.parse(fetchRITM);
var finalRITM = responseObj.extra_vars.RITM_NUMBER;
arr.push(finalRITM.toString());

}