What are the differences between using JSON arrays vs JSON objects?

ArraysJsonObject

Arrays Problem Overview


What are the difference and advantages of using JSON arrays:

{
   thing:[
     { },
     { }
   ]
}

versus JSON objects:

{
   thing:{
     { },
     { }
   }
}

Arrays Solutions


Solution 1 - Arrays

The difference between an array and an object is that

Objects are set up using a key and value like:

person.age = 15;

If the key value is a variable, then one could access it like:

var key = "age";
alert(person[key]);

Arrays use an integer[1] index and take a value.

player[1].score += 1000;

[1] Yes, I know, in JavaScript the integer index is really turned into a string behind the scenes. Ignore that. Think of arrays taking an integer value ESPECIALLY when you think of JSON.

Solution 2 - Arrays

Objects- key and value, Arrays- integer. When do you use this or that?

I think of arrays and objects as "is a/an" and "has a" respectively. Lets use "Fruit" as example.

Every item in fruit array is a type of fruit.

> array fruit : [orange, mango, banana]

. Arrays can contain objects,strings, numbers, arrays, but lets deal with only objects and arrays.

> array fruit : [orange:[], mango:{}, banana:{}]

. You can see that orange is an array too. It implies any item that goes int orange is a type of orange, say: bitter_orange, mandarin, sweet_orange.

for fruit object, any item in it is an attribute of fruit. thus the fruit has a

object fruit :{seed:{}, endocarp:{},flesh:{}}

This also implies that anything within the seed object should be property of seed, say: colour,

Solution 3 - Arrays

JSON arrays represent a collection of objects. In JS, theres a bunch of collection functions off of them such as slice, pop, push. Objects have just more raw data.

Solution 4 - Arrays

The second form you show is actually not valid JSON, as each of the objects in the "thing" object would need some sort or property name to access it by.

To answer your question, the difference is that in the first case, you would access the objects in "thing" using array access like obj.thing[0] or obj.thing[1]. In the second case, if you had proper property declarations you would access like obj.thing.property

Generally in JSON array are used to store a grouping of like items, while object are used to contain grouping of different properties for a single item.

Solution 5 - Arrays

JSON is primarily a language that allows serializing javascript objects into strings. So upon deserializing a JSON string you should get a javascript object structure. If your json deserializes into an object that stores 100 objects called object1 to object100 then that's going to be very inconvenient. Most deserializers will expect you to have known objects and arrays of known objects so that they can convert the strings into the actual object structure in the language you're using. Also this is a question that the philosophy of object oriented design would answer you.

Solution 6 - Arrays

A JSON object can be transformed using toJSON:

function kryptonite(key)
   {
   var replacement = {};
   for(var __ in this)
     {
     if(__ in alias)
       replacement[__] = this[__]
     }

   return replacement;
   }

var alias = {"Clark":"","phone":""};
var contact = {
               "Clark":"Kent",
               "Kal El":"Superman",
               "phone":"555-7777"
              }

contact.toJSON = kryptonite;

var foo = JSON.stringify(contact)

A JSON array can be transformed using map:

 var contact = {
               "Clark":"Kent",
               "Kal El":"Superman",
               "phone":"555-7777",
               "home":[{"present":"Metropolis"},{"past":"Krypton"},{"future":"Phantom Zone"}]

              }

 var filter = {"past":"","future":""}

 function junction(value, index)
   {
   for (var __ in filter) if(value[__]) return value[__]
   } 

var island = contact.home.map(junction);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionantonpugView Question on Stackoverflow
Solution 1 - ArraysJeremy J StarcherView Answer on Stackoverflow
Solution 2 - ArraysmegyewodiView Answer on Stackoverflow
Solution 3 - ArraysDaniel A. WhiteView Answer on Stackoverflow
Solution 4 - ArraysMike BrantView Answer on Stackoverflow
Solution 5 - ArraysnorbitheeviljesterView Answer on Stackoverflow
Solution 6 - ArraysPaul SweatteView Answer on Stackoverflow