Parsing complex JSON in script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 03:35 AM
Hi,
I have to read a complex JSON dynamically in my code which is a script being used in Virtual Agent. The JSON looks something like below:
{
"name": "Shreyoshi",
"description": "My Name",
"family": [
{
"id": "1",
"relationship": "Father",
},
{
"id": "2",
"relationship": "Mother",
}
]
}
In my script after reading the JSON, I am doing the below:
- Labels:
-
Communities

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 04:36 AM
Hi Shreyoshi,
To get the keys you can use below
var keys = Object.keys(JSON Object Here);
gs.info('Keys :' + keys);
For detailed information, please refer below article
Thanks & Regards,
Sharjeel
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 04:39 AM
Thanks but I dont need the key, I want the value inside the array which is being printed as Object

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 04:50 AM
Try this.
var obj = global.JSON.parse(reqTemp);
gs.info("JSON String " + obj);
for ( var key in obj){
gs.info(" Value of Family id is - " + obj.family[key].id);
gs.info(" Value of Family relationship is - " + obj.family[key].relationship);
}
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 04:48 AM
Hi,
An ideal way to parse JSON and get the value.
var text = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}';
var obj = JSON.parse(text);
gs.print(obj.birth);
In your case obj.family.id; etc
Hope this helps!
Please Mark as ✅ Correct if this solves your issue and also mark ???? Helpful if it helps resolve your problem.
Thanks,
Saji