How to retrieve sys_id from a JSON array

Andrei Radules1
Giga Contributor

Hello,

I am trying to retrieve with REST GET the sys_id from another SNOW instance based on the ticket number and then use PUT to update that ticket.

Then only issues that i have is that after the GET method it returns a JSON array. Can someone please explain how I can retrieve the sys_id and store in a var. from the JSON Array so that I can use the PUT method with it after.

The JSON array looks like this:

{"result":[{"parent":"","u_client_sys_id":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2019-05-31 20:54:06","child_incidents":"0","hold_reason":"","approval_history":"","number":"INC0010045","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://dev59748.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2019-05-31 20:54:06","sys_domain":{"link":"https://dev59748.service-now.com/api/now/table/sys_user_group/global","value":"global"},"state":"1","sys_created_by":"admin","knowledge":"false","order":"","calendar_stc":"","closed_at":"","cmdb_ci":"","delivery_plan":"","impact":"3","active":"true","work_notes_list":"","business_service":"","priority":"5","sys_domain_path":"/","rfc":"","time_worked":"","expected_start":"","opened_at":"2019-05-31 20:54:06","business_duration":"","group_list":"","work_end":"","caller_id":{"link":"https://dev59748.service-now.com/api/now/table/sys_user/e2826bf03710200044e0bfc8bcbe5dd7","value":"e2826bf03710200044e0bfc8bcbe5dd7"},"reopened_time":"","resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"6j6j","close_code":"","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","additional_assignee_list":"","business_stc":"","description":"j66j5j","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"6a03f4c5db75330003981fc7689619a8","contact_type":"","reopened_by":"","incident_state":"1","urgency":"3","problem_id":"","company":{"link":"https://dev59748.service-now.com/api/now/table/core_company/a66b1fb03710200044e0bfc8bcbe5d08","value":"a66b1fb03710200044e0bfc8bcbe5d08"},"reassignment_count":"0","activity_due":"","assigned_to":"","severity":"3","comments":"","approval":"not requested","sla_due":"","u_vendor_reference":"INC0010099","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","reopen_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":"","category":"inquiry"}]}

Appreciate the help !

1 ACCEPTED SOLUTION

SatheeshKumar
Kilo Sage
var response = '{"result":[{"parent":"","u_client_sys_id":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2019-05-31 20:54:06","child_incidents":"0","hold_reason":"","approval_history":"","number":"INC0010045","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://dev59748.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2019-05-31 20:54:06","sys_domain":{"link":"https://dev59748.service-now.com/api/now/table/sys_user_group/global","value":"global"},"state":"1","sys_created_by":"admin","knowledge":"false","order":"","calendar_stc":"","closed_at":"","cmdb_ci":"","delivery_plan":"","impact":"3","active":"true","work_notes_list":"","business_service":"","priority":"5","sys_domain_path":"/","rfc":"","time_worked":"","expected_start":"","opened_at":"2019-05-31 20:54:06","business_duration":"","group_list":"","work_end":"","caller_id":{"link":"https://dev59748.service-now.com/api/now/table/sys_user/e2826bf03710200044e0bfc8bcbe5dd7","value":"e2826bf03710200044e0bfc8bcbe5dd7"},"reopened_time":"","resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"6j6j","close_code":"","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","additional_assignee_list":"","business_stc":"","description":"j66j5j","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"6a03f4c5db75330003981fc7689619a8","contact_type":"","reopened_by":"","incident_state":"1","urgency":"3","problem_id":"","company":{"link":"https://dev59748.service-now.com/api/now/table/core_company/a66b1fb03710200044e0bfc8bcbe5d08","value":"a66b1fb03710200044e0bfc8bcbe5d08"},"reassignment_count":"0","activity_due":"","assigned_to":"","severity":"3","comments":"","approval":"not requested","sla_due":"","u_vendor_reference":"INC0010099","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","reopen_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":"","category":"inquiry"}]}';


var result = JSON.parse(response).result[0].opened_by.value;

gs.print(result);



 

var result = JSON.parse(response).result[0].opened_by.value;

gs.print(result);

 

this will give the sysid avaialble in the response!!.

 

-satheesh

View solution in original post

4 REPLIES 4

SatheeshKumar
Kilo Sage
var response = '{"result":[{"parent":"","u_client_sys_id":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2019-05-31 20:54:06","child_incidents":"0","hold_reason":"","approval_history":"","number":"INC0010045","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://dev59748.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2019-05-31 20:54:06","sys_domain":{"link":"https://dev59748.service-now.com/api/now/table/sys_user_group/global","value":"global"},"state":"1","sys_created_by":"admin","knowledge":"false","order":"","calendar_stc":"","closed_at":"","cmdb_ci":"","delivery_plan":"","impact":"3","active":"true","work_notes_list":"","business_service":"","priority":"5","sys_domain_path":"/","rfc":"","time_worked":"","expected_start":"","opened_at":"2019-05-31 20:54:06","business_duration":"","group_list":"","work_end":"","caller_id":{"link":"https://dev59748.service-now.com/api/now/table/sys_user/e2826bf03710200044e0bfc8bcbe5dd7","value":"e2826bf03710200044e0bfc8bcbe5dd7"},"reopened_time":"","resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"6j6j","close_code":"","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","additional_assignee_list":"","business_stc":"","description":"j66j5j","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"6a03f4c5db75330003981fc7689619a8","contact_type":"","reopened_by":"","incident_state":"1","urgency":"3","problem_id":"","company":{"link":"https://dev59748.service-now.com/api/now/table/core_company/a66b1fb03710200044e0bfc8bcbe5d08","value":"a66b1fb03710200044e0bfc8bcbe5d08"},"reassignment_count":"0","activity_due":"","assigned_to":"","severity":"3","comments":"","approval":"not requested","sla_due":"","u_vendor_reference":"INC0010099","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","reopen_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":"","category":"inquiry"}]}';


var result = JSON.parse(response).result[0].opened_by.value;

gs.print(result);



 

var result = JSON.parse(response).result[0].opened_by.value;

gs.print(result);

 

this will give the sysid avaialble in the response!!.

 

-satheesh

Thank you, it worked !!  I just needed to use .sys_id

Oleg
Mega Sage

I suppose that you use sn_ws.RESTMessageV2 to retrieve data from another ServiceNow instance. The code looks close to the following:

var request = new sn_ws.RESTMessageV2();
var incidentNumber = "INC0011018";
request.setEndpoint('https://youinstance.service-now.com/api/now/table/incident?' +
    'sysparm_query=number%3D' + incidentNumber + '&sysparm_limit=1');
request.setHttpMethod('GET');

//Eg. UserName="admin", Password="PasswordOfAdmin" for this code sample.
var user = 'admin';
var password = 'PasswordOfAdmin';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept", "application/json");

var response = request.execute();
if (!response.haveError()) {
    var body = response.getBody();
    //gs.print(body);
    var data = JSON.parse(body);
    gs.print(data.result[0].sys_id);
}

then you will need just use data.result[0].sys_id to access sys_id of the ticket (see the last line of the code).

In general, I'd recommend you to format the body of response in https://jsonlint.com/. After you will see the data in good formatted form the corresponding expression like .result[0].sys_id will be very easy to build.

Thank you very much for the advice !