How to get the values from json of array

Sai25
Giga 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

Ankur Bawiskar
Tera Patron
Tera Patron

@Sai25 

try this

As of now I considered items[0] as only 1 object is there; you can enhance it further

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[0].systemDevices.length; i++) {

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

}

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.

Hi ,

If I want to get the autoDevices and InheritedDevices do I need add two another for loops? can you please confirm or another simplest way we can do instead of this many for loops.

 

Thank You,

Sai.

@Sai25 
I tried to format your JSON string, but I encountered an error. I need the JSON to be formatted so that I can understand the JSON structure.

Can you give me a formatted json string ?

 

 

newhand_0-1683536999614.png

 

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