Parsing complex JSON in script

Shreyoshi1
Giga Contributor

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:

var obj = new global.JSON().decode(reqTemp.toString());
gs.info("JSON String " + obj);
for ( var key in obj)
{
gs.info(" key is : " + key + " and value for key is " + obj[key]);
}
For the key="family", the value is being printed as [object Object], how can I read the JSON within the "family" array dynamically, without having to know the exact key?
 
Can anyone help me on this please.
 
Thanks

 

10 REPLIES 10

Willem
Giga Sage
Giga Sage

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.

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

Brad Bowman
Kilo Patron
Kilo Patron

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

Shreyoshi1
Giga Contributor

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.

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