Parse Nested JSON Array and objects

triciav
Kilo Sage

I have a RestMessage that I am getting back the JSON as below,

How do I parse this to get the SerialNumbers?

Thank you

find_real_file.png

{
"Devices":[
{
"EasIds":{
"EasId":[
"XXPI8PIKDD4LP5ID797BVKSGKO",
"XXFDA7DB295B406C9933FA634B190B77"
]
},
"Udid":"00000000-000A6C940E30802E",
"SerialNumber":"xxxxxxxxx",

1 ACCEPTED SOLUTION

your obj is the data

//try the following code

 

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

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it seems incomplete json. can you share complete one?

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

vkachineni
Kilo Sage
Kilo Sage

//try. Although the JSON is incomplete, i am guessing the first serial number will be.

 

object.Devices[0].SerialNumber;
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

This is the JSON, There could be more than 1 SerialNumber devices so I would need to loop through 
to get each one
{
"Devices":[
{
"EasIds":{
"EasId":[
"XXPI8PIKDD4LP5ID797BVKSGKO",
"XXFDA7DB295B406C9933FA634B190B77"
]
},
"Udid":"00008030-000A6C940E30802E",
"SerialNumber":"XXXXXXX",
"MacAddress":"XXX71841C1C",
"Imei":"311895101261879",
"EasId":"",
"AssetNumber":"0000000-000A6C940E30802E",
"DeviceFriendlyName":"jdoe iPhone iOS 13.2.3 N70Q",
"LocationGroupId":{
"Id":{
"Value":98072
},
"Uuid":"18726ff8-6a4c-4b4f-989c-7ef45e7b0082",
"Name":"Production"
},
"LocationGroupName":"Production",
"UserId":{
"Id":{
"Value":195275
},
"Uuid":"17fb71b7-79dd-4888-b712-e1216835cce9",
"Name":"Jane Doe"
},
"UserName":"jdoe",
"DataProtectionStatus":0,
"UserEmailAddress":"jdoe@example.com",
"Ownership":"E",
"PlatformId":{
"Id":{
"Value":2
},
"Name":"Apple"
},
"Platform":"Apple",
"ModelId":{
"Id":{
"Value":1
},
"Name":"iPhone 11 Pro Max (512 GB Midnight Green)"
},
"Model":"iPhone 11 Pro Max (512 GB Midnight Green)",
"OperatingSystem":"13.2.3",
"PhoneNumber":"+12229991111",
"LastSeen":"2019-12-06T18:42:07.303",
"EnrollmentStatus":"Enrolled",
"ComplianceStatus":"Compliant",
"CompromisedStatus":false,
"LastEnrolledOn":"2019-10-01T13:15:12.527",
"LastComplianceCheckOn":"2019-12-04T18:12:56.440",
"LastCompromisedCheckOn":"2019-12-06T18:26:02.153",
"IsSupervised":false,
"VirtualMemory":0,
"DeviceCapacity":506008895488.0,
"AvailableDeviceCapacity":200861466624.0,
"IsDeviceDNDEnabled":false,
"IsDeviceLocatorEnabled":false,
"IsCloudBackupEnabled":false,
"IsActivationLockEnabled":true,
"IsNetworkTethered":false,
"BatteryLevel":"85 %",
"IsRoaming":false,
"SystemIntegrityProtectionEnabled":false,
"ProcessorArchitecture":0,
"TotalPhysicalMemory":0,
"AvailablePhysicalMemory":0,
"DeviceCellularNetworkInfo":[
{
"CarrierName":"",
"CardId":"11114103272344405762",
"PhoneNumber":"+12229991111
",
"DeviceMCC":{
"SIMMCC":"",
"CurrentMCC":""
},
"IsRoaming":false
}
],
"EnrollmentUserUuid":"00000000-0000-0000-0000-000000000000",
"Id":{
"Value":655723
},
"Uuid":"1115d471-aed3-4d11-b324-22e6c35775d5"
}
],
"Page":0,
"PageSize":500,
"Total":1
}

vkachineni,

That worked so I need to figure out how to loop through to get all the SerialNumbers

How can I achieve that

Thank you