Parsing nested JSON

Krischan
Giga Guru

When I send a REST message to another system, I do get a response back, which I'd like to work with (e.g. read out the INC number).

However, I seem to be not able to do so for some reason. 

I assume it is a structure of the JSON vs. my Script issue and maybe just need a quick finger point.

{"result":{"parent":"","made_sla":"true","caused_by":""<more>}}

What I'm trying to do is, parsing the JSON object using:

var parsedResponse = JSON.parse(responseBody);

but putting it out in the log just shows me another "object" 

Parsed Response: [object Object] Now I tried just parsing that object but can't seem to get anywhere. Is there a clever (and more simple) way to do this? 

I mean it's a simple response from another ServiceNow instance.

1 ACCEPTED SOLUTION

harishdasari
Tera Guru

Hi ChristianRatzlaff,

try the code as shown below, it will print the JSON objects.

var parser = new JSONParser();
var parsedData = parser.parse(responseBody);
var result = parsedData.result.made_sla;

gs.print(result);

Thank you.

View solution in original post

2 REPLIES 2

Munender Singh
Mega Sage

Hi,

Actually you can fetch the objects of JSON using this way:

v

 

var data = '{"result":{"parent":"","made_sla":"true","caused_by":""<more>}}';       // Demo json Data

var jsData= JSON.parse(data);   // convert json object into javascript object

var madeSla = jsData["result"].made_sla;

var causedBy = jsData["result"].caused_by;

gs.log(madeSla +"result"+ causedBy);

But,you can't print the complete JSON this way

Regards,

Munender

harishdasari
Tera Guru

Hi ChristianRatzlaff,

try the code as shown below, it will print the JSON objects.

var parser = new JSONParser();
var parsedData = parser.parse(responseBody);
var result = parsedData.result.made_sla;

gs.print(result);

Thank you.