Trouble accessing a value within a JSON string.

Joined
Jul 23, 2020
Messages
4
Reaction score
1
Hi folks,

Long time since my last post. I always loved this forum because of all you guys and how you have helped me in the past. It's been a while since I put on my coding hat and now I am working in ServiceNow and trying to access a value within a REST response that is formatted as JSON.

Here is the JSON string I am trying to pull from:

JSON:
{"import_set":"ISET0156452","staging_table":"x_insmc_intapis_std_inbound_incident","result":[{"transform_map":"STD Inbound Incident","table":"incident","display_name":"number","display_value":"INC1540824","record_link":"https://somedevserver.service-now.com/api/now/table/incident/a636798cdb23a1105d34252b1396193d","status":"updated","sys_id":"a636798cdb23a1105d34252b1396193d"}]}

I am trying to pull the value of the "display_value" and "sys_id" keys with the following code but when I test, the variables are blank:

JavaScript:
(function execute(inputs, outputs) {
  var responseBody = JSON.parse(inputs.response);
  if(inputs.status!=201){
       var errorMsg =  responseBody.error.message;
    var errorDetail = responseBody.error.detail;
    throw "Error creating incident. Message: "+errorMsg + " Details:"+errorDetail;
  } else {
    var result = responseBody.result;
    outputs.remote_incident_number = result.display_value;
    outputs.remote_sys_id = result.sys_id;
  }
})(inputs, outputs);

I loaded the JSON into an online parser and did notice an additional level under the result key. Does this have to be specified in my code somehow?

1686945934343.png


I tried setting my "result" variable as follows:
var result = responseBody.result.0;

But that threw an error "missing ; before statement".

I am stumped as to why these values are not populating. Any help is appreciated.

Thank you,
Ken
 
Joined
Jul 23, 2020
Messages
4
Reaction score
1
SOLVED:

JavaScript:
(function execute(inputs, outputs) {
  var responseBody = JSON.parse(inputs.response);
  if(inputs.status!=201){
       var errorMsg =  responseBody.error.message;
    var errorDetail = responseBody.error.detail;
    throw "Error creating incident. Message: "+errorMsg + " Details:"+errorDetail;
  } else {
    var result = responseBody.result;
    outputs.remote_incident_number = result[0]["display_value"];
    outputs.remote_sys_id = result[0]["sys_id"];
  }
})(inputs, outputs);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,661
Latest member
NadineBour

Latest Threads

Top