How to access JSON array object in server-side script?

Kenneth3
Tera Guru

Hi,

I have the following (JSON) array of objects and code (which I get from a REST API - see first block) and want to access / iterate through those items, but I am encountering some issues (see second block).

The gs.info(objComputer); gives the output: [object Object] and the gs.info('Object count: ' + objComputer.length); gives the output: Object count: undefined.

var computer = {"computer":[{"Body":{"BiosSerialNumber":"3b200898-6811-3240-a99d-9199181bf627","Manufacturer":"IBM","Name":"vmw0124"}},{"Body":{"BiosSerialNumber":"925a33b5-f18c-39ca-860d-fdecaba9f150","Manufacturer":"IBM","Name":"vmw0125"}}]};

var strComputer = JSON.stringify(computer, null, 2);  // Convert outputs to string
gs.info('String count: ' + strComputer.length);
gs.info(strComputer);

var objComputer = JSON.parse(strComputer);  // Convert outputs string to an object
gs.info(objComputer);
gs.info('Object count: ' + objComputer.length);

for (var i = 0; i < objComputer.length; i++) {
    gs.info('Record - ' + i);
    gs.info(computer[i].Name);
    gs.info(computer[i].BiosSerialNumber);
}

 

*** Script: String count: 346
*** Script: {
  "computer": [
    {
      "Body": {
        "BiosSerialNumber": "3b200898-6811-3240-a99d-9199181bf627",
        "Manufacturer": "IBM",
        "Name": "vmw0124"
      }
    },
    {
      "Body": {
        "BiosSerialNumber": "925a33b5-f18c-39ca-860d-fdecaba9f150",
        "Manufacturer": "IBM",
        "Name": "vmw0125"
      }
    }
  ]
}
*** Script: [object Object]
*** Script: Object count: undefined


What is the correct way to access and loop through the data?


Best regards

6 REPLIES 6

suvro
Mega Sage
Mega Sage

Try this

 

var computer = {"computer":[{"Body":{"BiosSerialNumber":"3b200898-6811-3240-a99d-9199181bf627","Manufacturer":"IBM","Name":"vmw0124"}},{"Body":{"BiosSerialNumber":"925a33b5-f18c-39ca-860d-fdecaba9f150","Manufacturer":"IBM","Name":"vmw0125"}}]};

var strComputer = JSON.stringify(computer, null, 2);  // Convert outputs to string
gs.info('String count: ' + strComputer.length);
gs.info(strComputer);

var objComputer = JSON.parse(strComputer);  // Convert outputs string to an object
gs.info(objComputer.computer);
gs.info('Object count: ' + objComputer.computer.length);

for (var i = 0; i < objComputer.computer.length; i++) {
    gs.info('Record - ' + i);
    gs.info(objComputer.computer[i].Body.Name);
    gs.info(objComputer.computer[i].Body.BiosSerialNumber);
}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

when you get response from REST Message it's a string

So directly parse it

Regards
Ankur

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

Hi,

something like this

var str = '{"computer":[{"Body":{"BiosSerialNumber":"3b200898-6811-3240-a99d-9199181bf627","Manufacturer":"IBM","Name":"vmw0124"}},{"Body":{"BiosSerialNumber":"925a33b5-f18c-39ca-860d-fdecaba9f150","Manufacturer":"IBM","Name":"vmw0125"}}]}';

var parsedData = JSON.parse(str);

for(var i=0;i<parsedData.computer.length;i++) {
     gs.info(parsedData.computer[i].Body.Name);
    gs.info(parsedData.computer[i].Body.BiosSerialNumber);
}

Regards
Ankur

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

Hi Ankur, thank you.

After the response of the REST step I am using in the Action flow also the JSON Parser Step. In that step the root in the output target structure has the data type of 'Object'. Is it even than still a string?

find_real_file.png 

Regards,
Kenneth