Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How to get the values from json of array

Sai25
Tera Guru

Hi Team,

Can you please how to get the values from Json of array, below is the example.

I am able to the values from first object i.e. items, but from array from object I am unable to get the values.

 

var obj = {
"total" : 32,
"items" : [ {
"id" : 1,
"name" : "32.435.3.6",
"displayName" : "Mgmt-KJS-AZ05_collector",
"deviceRouter" : 0,
"relatedRouterId" : -1,
"currentRouterId" : 6,
"preferredRouterId" : 6,
"disableAlerting" : false,
"customDevices" : [ {
"name" : "system.devices",
"value" : "router"
} ],
"upSeconds" : 34309,
"deletedMs" : 0,
"toMs" : 0,
"hasResource" : false,
"ancestorHModule" : false,
"systemDevices" : [ {
"name" : "system.Devices",
"value" : "8.00GB"
}, {
"name" : "system.enablenetflow",
"value" : "false"
}, {
"name" : "system.systemtype",
"value" : "x64-based PC"
},"autoDevices" : [ {
"name" : "auto.operational",
"value" : "true"
}, {
"name" : "autonames",
"value" : "local"
}, {
"name" : "auto.os.version",
"value" : "14.0.14263"
}, "inheritedDevices" : [ {
"name" : "group",
"value" : "Team"
}, {
"name" : "community",
"value" : "Used"
}, {
"name" : "pass",
"value" : "passed"
},
}
]
}

 

 

var a = JSON.stringify(obj);

var parser = new JSONParser();

var parsed = parser.parse(a);


var length = parsed.length;
gs.log(length);

var str1 = [];
for (i = 0; i <parsed.items.length; i++) {
gs.log("Items===="+parsed.items[i].sdtStatus);

gs.log("system devices"+parsed.items[i].systemDevices[0].name);//I want to get those values at a time , right now getting only one record value.

}

1 ACCEPTED SOLUTION

newhand
Mega Sage

@Sai25  
answer from chatGPT 😀

 

 

var parsed = JSON.parse(JSON.stringify(obj)); // No need for JSONParser

var length = parsed.items.length;
gs.log(length);

for (var i = 0; i < length; i++) {
    var item = parsed.items[i];
    gs.log("Item ====" + item.displayName);

    var systemDevices = item.systemDevices;
    var systemDevicesLength = systemDevices.length;
    for (var j = 0; j < systemDevicesLength; j++) {
        var systemDevice = systemDevices[j];
        gs.log("system devices name ====" + systemDevice.name);
        gs.log("system devices value ====" + systemDevice.value);
    }
}

 

Please mark my answer as correct and helpful based on Impact.

View solution in original post

6 REPLIES 6

HI @newhand 

Actually sorry for that, I cannot send you the original data as it has confidential information.

 

can you please help me with the above one only.

@Sai25 
To iterate over an array or list, a loop is needed. Alternatively, you can try using forEach.

 

 

 

var myJsonArray = [
  { "id": 1, "name": "John" },
  { "id": 2, "name": "Jane" },
  { "id": 3, "name": "Bob" }
];

myJsonArray.forEach(function(item) {
  gs.log(item.id+ item.name);
});

 

 

 

 

Please mark my answer as correct and helpful based on Impact.