How to format JSONArray with nested arrays

954 Views Asked by At

I wannt to access the fields in my JSONArray. The nested brackets inside the JSONArray is very troublesome. I can't see how this format is an acceptable JSONArray return value. I get an JSONException when I try to access a field (eg "rethink3__Address__c") using getJSONObject().

 [
   [
      {
         "attributes":{
            "type":"rethink3__Listing__c",
            "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
         },
         "rethink3__Address__c":null,
         "Alarm_Code__c":null,
         "rethink3__Bathrooms__c":0,
         "rethink3__Bedrooms__c":0,
         "rethink3__Size__c":0,
         "Lock_Box_Code__c":null,
         "Lock_Box_Location_Notes__c":null,
         "_soupEntryId":1,
         "_soupLastModifiedDate":1537657104801
      }
   ],
   [
      {
         "attributes":{
            "type":"rethink3__Listing__c",
            "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
         },
         "rethink3__Address__c":null,
         "Alarm_Code__c":null,
         "rethink3__Bathrooms__c":0,
         "rethink3__Bedrooms__c":0,
         "rethink3__Size__c":0,
         "Lock_Box_Code__c":null,
         "Lock_Box_Location_Notes__c":null,
         "_soupEntryId":1,
         "_soupLastModifiedDate":1537657104801
      }
   ]
]
1

There are 1 best solutions below

1
On BEST ANSWER

A [] = json array and {} = json object. So try this.

let myArray = [
    [
        {
            "attributes":{
                "type":"rethink3__Listing__c",
                "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
            },
            "rethink3__Address__c":null,
            "Alarm_Code__c":null,
            "rethink3__Bathrooms__c":0,
            "rethink3__Bedrooms__c":0,
            "rethink3__Size__c":0,
            "Lock_Box_Code__c":null,
            "Lock_Box_Location_Notes__c":null,
            "_soupEntryId":1,
            "_soupLastModifiedDate":1537657104801
        }
    ],
    [
        {
            "attributes":{
                "type":"rethink3__Listing__c",
                "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
            },
            "rethink3__Address__c":null,
            "Alarm_Code__c":null,
            "rethink3__Bathrooms__c":0,
            "rethink3__Bedrooms__c":0,
            "rethink3__Size__c":0,
            "Lock_Box_Code__c":null,
            "Lock_Box_Location_Notes__c":null,
            "_soupEntryId":1,
            "_soupLastModifiedDate":1537657104801
        }
    ]
];

myArray.forEach((myNestedArray)=>{
    let obj = myNestedArray[0]
    console.log(obj.attributes.type);
    console.log(obj._soupLastModifiedDate);
})