How to extract Data from JSON Payload?

snow_04
Giga Contributor

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!!

 

 

17 REPLIES 17

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!

 

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes Ankur that's correct.I am using a workflow which has multiple activities where I am using workflow scratchpad variables to execute rest service and then pass values to another activity where I need to extract the value which is within Response Json which is again enclosed within a parent json. Is there a way we can do?

Hi,

while storing the value in workflow scratchpad variable can you use toString() and the try to use that in another actvity

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

DirkRedeker
Mega Sage

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));

 

find_real_file.png

 

Let me know, if that answers your question and mark my ansswer as correct and helpful, please.

Thanks & BR

Dirk