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.

how to get the REST API code

hemantnh
Tera Expert

i am getting the rest api response like below 

 

Response Body: {"id":"dsajnddna","id2":"orn","id3":"orndawdw"}

 

now i want get the id2 value from this body, i am unable to get the id2 value. please help mee

1 ACCEPTED SOLUTION

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

If your response body is a JSON string:

var responseBody = '{"id":"dsajnddna","id2":"orn","id3":"orndawdw"}';
var obj = JSON.parse(responseBody);

var id2 = obj.id2; // "orn"

 

If your response body is already a JSON object:

var id2 = responseBody.id2;

 

If you need to guard against missing fields:

var id2 = (obj && obj.id2) ? obj.id2 : null;

 

View solution in original post

1 REPLY 1

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

If your response body is a JSON string:

var responseBody = '{"id":"dsajnddna","id2":"orn","id3":"orndawdw"}';
var obj = JSON.parse(responseBody);

var id2 = obj.id2; // "orn"

 

If your response body is already a JSON object:

var id2 = responseBody.id2;

 

If you need to guard against missing fields:

var id2 = (obj && obj.id2) ? obj.id2 : null;