Parsing values from multi-level JSON Array

Immanuel Kant
Kilo Contributor

Hi,

I am retrieving data from Google Admin. The goal is to retrieve the most recent device users from the customer and update their hardware assets in our instance with the most recent user periodically.

The response body I get looks like this: 

{
    "chromeosdevices": [
        {
            "serialNumber": "F88B473",
            "lastSync": "2020-10-25T07:01:21.772Z",
            "recentUsers": [
                {
                    "type": "USER_TYPE_MANAGED",
                    "email": "madeup_name@mail.com"
                }
            ]
        },

        {
            "serialNumber": "5CD13786X9",
            "lastSync": "2020-10-14T07:29:47.104Z"
        },
        {
            "serialNumber": "NXHKLED00J08132D917600",
            "lastSync": "2020-11-03T10:09:58.319Z",
            "recentUsers": [
                {
                    "type": "USER_TYPE_MANAGED",
                    "email": "madeup_name2@mail.com"
                },
                {
                    "type": "USER_TYPE_MANAGED",
                    "email": "madeup_name3@mail.com"
                }
            ]
        }        
    ]
}

This response body has been shortened for brevity. I retrieve all their devices at once. What I need to do is iterate through the chromedevices array and each individual recentUsers array (the first user in every recentUsers is the most recent one), so I can put serialNumber and email together and update our records.

I've gotten so far as getting my hands on the serial numbers using a regular for loop:

var obj = JSON.parse(responseBody);

for (i = 0; i < obj.chromeosdevices.length; i++) {	
    gs.print(obj.chromeosdevices[i].serialNumber);
 }

But I am stumped as to how I can get my hands on the emails and put them together. 

Anyone got any pointers? 🙂

5 REPLIES 5

sachin_namjoshi
Kilo Patron
Kilo Patron

Use below code to parse serial number from your JSON object

 

var json = new global.JSON();
var obj = json.decode(responseBody);
for(i = 0; i<= obj.Devices.length-1; i++)
{
gs.info(obj.chromeosdevices[i].serialNumber);
}

 

Regards,

Sachin

Hi Sachin

Thank you for the response, however I've already managed to parse the serial numbers. The challenge is to parse the most recent email from the recentUsers arrays paired with the serial number 🙂

You can use below code to parse most recent email

 

 

var obj = {
    "chromeosdevices": [
        {
            "serialNumber": "F88B473",
            "lastSync": "2020-10-25T07:01:21.772Z",
            "recentUsers": [
                {
                    "type": "USER_TYPE_MANAGED",
                    "email": "madeup_name@mail.com"
                }
            ]
        },

        {
            "serialNumber": "5CD13786X9",
            "lastSync": "2020-10-14T07:29:47.104Z"
        },
        {
            "serialNumber": "NXHKLED00J08132D917600",
            "lastSync": "2020-11-03T10:09:58.319Z",
            "recentUsers": [
                {
                    "type": "USER_TYPE_MANAGED",
                    "email": "madeup_name2@mail.com"
                },
                {
                    "type": "USER_TYPE_MANAGED",
                    "email": "madeup_name3@mail.com"
                }
            ]
        }        
    ]
};

gs.print(obj.chromeosdevices[0].recentUsers.email);

 

Regards,

Sachin

Thanks again Sachin, however this gives me 'undefined'. I also need to loop through all of them.