How to extract Data from JSON Payload?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2019 11:11 PM
Hi,
How can we extract data from response coming in JSON format? Like for example as mentioned below:
{"state":2,"httpCode":200,"response":"{\"Ticket\" : \"061cech41hh7e27c86f88b08c8fd2a\",\"StatusCode\" : \"201 Created\",\"Message\" : \"Record Created.\"}"}
Currently what I am doing in background script is as below:
var str = {"state":2,"httpCode":200,"response":"{\"Ticket\" : \"061cech41hh7e27c86f88b08c8fd2a\",\"StatusCode\" : \"201 Created\",\"Message\" : \"Record Created.\"}"};
gs.info(JSON.stringify(str.status)); -- Getting Correct Output as "2".
Can someone please assist on how to extract the elements present within response attribute JSON, like I want to extract objects like "Ticket" , "StatusCode" which are within the Response Object etc.
Currently if I do something as below it gives me undefined:
gs.info(JSON.stringify(str.response.IncidentId));
Kindly assist!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 12:41 AM
Your example works because you have changed the line with the json string from a STRING to actually define the object as a json object.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 12:39 AM
You are going the wrong way.
To access individual elements in the json string you need to parse it to an object not stringify it ( it is already a string ).
So instead you need to do this:
var obj = JSON.parse(str);
gs.info(obj.state);
gs.info(obj.response.xxxxx);
See https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_JSON-parse_S
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 03:25 AM
Hi Tommy,
Agree with you, but I already tried JSON.parse and it throws an error to me as stated below:
"EcmaError: Unexpected token in object literal"
Do you know why this does not works?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 03:40 AM
It looks like you've converted a JSON object to string and you're now trying to parse that string value into an object. When you use toString() or similar on a JSON object it inserts a bunch of \ characters as escapes and some " for good measure, when you try to parse it into an object it throws errors because those \ characters don't make sense in an object literal.
To test this in the back ground you need to remove the characters the toString() method inserted, the actual JSON payload would look like this:
{
"state":2,
"httpCode":200,
"response":
{
"Ticket" : "061cech41hh7e27c86f88b08c8fd2a",
"StatusCode" : "201 Created",
"Message" : "Record Created."
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 04:54 AM
Hi David,
I think this is what is happening as you mentioned.
My Bad in posting the question incorrectly.
I am getting a response from a 3rd party as mentioned below:
{"Ticket" : "Value","Status" : "201 Created","Message" : "Record Created."}
I have a JSON Object with parameters as mentioned below where I am pushing each of the values I am getting in the Response from 3rd Party as below:
var obj={
"status" : "Value(Either setting it to True or False",
"httpCode": "As obtained from Response after Rest Service execution",
"response" : "responseBody(which is mentioned above in Bold)"
}
I need to extract the Ticket attribute from the response JSON coming from 3rd party which is enclosed again within "response" attribute of obj and looks like as below when I do JSON.stringify(obj)
{"state":2,"httpCode":200,"response":"{\"Ticket\" : \"061cech41hh7e27c86f88b08c8fd2a\",\"Status\" : \"201 Created\",\"Message\" : \"Record Created.\"}"};
Can you kindly assist on how to retrieve the Ticket which is within the Response object as shown above.
Kindly assist!