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:57 AM
I have it working as Background script. Tried changing it to your situation. Can you confirm you want to print out the Key-Value pairs from JSON object the Key- Family?
var obj = new global.JSON().decode(reqTemp.toString());
var famArr = obj["family"];
for (var i = 0; i < famArr.length; i++) {
obj2 = famArr[i]
for ( var key in obj2){
gs.info(" key is : " + key + " and value for key is " + obj2[key]);
}
}
I read somewhere JSON decode is not the best. So you can try this as well:
var obj = new global.JSON.parse(reqTemp.toString());
var famArr = obj["family"];
for (var i = 0; i < famArr.length; i++) {
obj2 = famArr[i]
for ( var key in obj2){
gs.info(" key is : " + key + " and value for key is " + obj2[key]);
}
}
Let me know if this works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 05:18 AM
To include your other Keys as well:
var obj = new global.JSON().decode(str.toString());
for (var key in obj) {
if (key == 'family') {
var famArr = obj["family"];
for (var i = 0; i < famArr.length; i++) {
obj2 = famArr[i]
for (var key in obj2) {
gs.info(" key is : " + key + " and value for key is " + obj2[key]);
}
}
}
else {
gs.info(" key is : " + key + " and value for key is " + obj[key]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2020 05:12 AM
Building off of your current script, this will dynamically show the family array
var obj = new global.JSON().decode(str.toString());
for(var key in obj){
if(key == 'family'){
for(var familykey in obj.family){
for(var familykey2 in obj.family[familykey]){
gs.info(" key is : " + key + " and value for key is " + obj.family[familykey][familykey2]);
}
}
}
else{
gs.info(" key is : " + key + " and value for key is " + obj[key]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2020 02:02 AM
Thanks for all your responses but the scenario I have is completely dynamic where I won't know the value of a key(family as I have given in the example). I need to read the JSON as key-value pair and need to repeat it for the nested complex structures. As of now, as soon as I get a complex structure in the value of a key(like family), the value is printed as [Object object]. I want a way to go inside and parse the object.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2020 02:09 AM
You could try it like this:
var obj = new global.JSON().decode(str.toString());
for (var key in obj) {
if (typof obj[key] == "object") {
var unknownArr = obj[key];
for (var i = 0; i < unknownArr.length; i++) {
obj2 = unknownArr[i]
for (var key in obj2) {
gs.info(" key is : " + key + " and value for key is " + obj2[key]);
}
}
}
else {
gs.info(" key is : " + key + " and value for key is " + obj[key]);
}
}
However, that will only work if there are not other objects or arrays in the JSON