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 04:53 AM
Hi Ankur,
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.\"}"};
I tried executing your script in my Server side script and it returns to me undefined where as in background Script your code works fine.
Kindly assist!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2019 05:39 AM
Hi,
So you are getting the response object which is a json and putting that in another json
Then trying to parse it?
Can you share the script how it is working now?
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2019 06:11 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2019 06:16 AM
Hi,
while storing the value in workflow scratchpad variable can you use toString() and the try to use that in another actvity
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2019 12:01 AM
Hi
I changed the example a bit, and also the JSON data inside (have a close look).
For me it works (see code and screenshot below:
var str = {"state":2,"httpCode":200,"response":{"Ticket" : "061cech41hh7e27c86f88b08c8fd2a","StatusCode" : "201 Created","Message" : "Record created"}};
gs.info(JSON.stringify(str.state));
gs.info(JSON.stringify(str.httpCode));
gs.info(JSON.stringify(str.response.Ticket));
var str2 = str.response;
gs.info(JSON.stringify(str2.Ticket));
Let me know, if that answers your question and mark my ansswer as correct and helpful, please.
Thanks & BR
Dirk