127

After having a short look at Google I found this link that describes the difference, yet from a syntax point of view.

When would one be preferred over the other in a programming scenario?

Luke Taylor
  • 9,481
  • 13
  • 41
  • 73

9 Answers9

220

When you are working with JSON data in Android, you would use JSONArray to parse JSON which starts with the array brackets. Arrays in JSON are used to organize a collection of related items (Which could be JSON objects).
For example: [{"name":"item 1"},{"name": "item2"} ]

On the other hand, you would use JSONObject when dealing with JSON that begins with curly braces. A JSON object is typically used to contain key/value pairs related to one item. For example: {"name": "item1", "description": "a JSON object"}

Of course, JSON arrays and objects may be nested inside one another. One common example of this is an API that returns a JSON object containing some metadata alongside an array of the items matching your query:

{"startIndex": 0, "data": [{"name": "item 1"},{"name": "item2"} ]}
LeoDog896
  • 3,472
  • 1
  • 15
  • 40
Eric Levine
  • 13,536
  • 5
  • 49
  • 49
125

The difference is the same as a (Hash)Map vs List.

JSONObject:

  • Contains named values (key->value pairs, tuples or whatever you want to call them)
    • like {ID : 1}
  • Order of elements is not important
    • a JSONObject of {id: 1, name: 'B'} is equal to {name: 'B', id: 1}.

JSONArray:

  • Contains only series values
    • like [1, 'value']
  • Order of values is important
    • array of [1,'value'] is not the same as ['value',1]

Example

JSON Object --> { "":""}

JSON Array --> [ , , , ]

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}
Mahesh K
  • 1,689
  • 3
  • 23
  • 36
Aleksandr Panzin
  • 1,987
  • 1
  • 14
  • 11
26

Best programmatically Understanding.

when syntax is {}then this is JsonObject

when syntax is [] then this is JsonArray

A JSONObject is a JSON-like object that can be represented as an element in the JSONArray. JSONArray can contain a (or many) JSONObject

Hope this will helpful to you !

Community
  • 1
  • 1
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
7

I always use object, it is more easily extendable, JSON array is not. For example you originally had some data as a json array, then you needed to add a status header on it you'd be a bit stuck, unless you'd nested the data in an object. The only disadvantage is a slight increase in complexity of creation / parsing.

So instead of

[datum0, datum1, datumN]

You'd have

{data: [datum0, datum1, datumN]}

then later you can add more...

{status: "foo", data: [datum0, datum1, datumN]}
Adam
  • 35,919
  • 9
  • 100
  • 137
7

To understand it in a easier way, following are the diffrences between JSON object and JSON array:

Link to Tabular Difference : https://i.stack.imgur.com/GIqI9.png

JSON Array

1. Arrays in JSON are used to organize a collection of related items
   (Which could be JSON objects)
2.  Array values must be of type string, number, object, array, boolean or null
3.  Syntax: 
           [ "Ford", "BMW", "Fiat" ]
4.  JSON arrays are surrounded by square brackets []. 
    **Tip to remember**  :  Here, order of element is important. That means you have 
    to go straight like the shape of the bracket i.e. straight lines. 
   (Note :It is just my logic to remember the shape of both.) 
5.  Order of elements is important. Example:  ["Ford","BMW","Fiat"] is not 
    equal to ["Fiat","BMW","Ford"]
6.  JSON can store nested Arrays that are passed as a value.

JSON Object

1.  JSON objects are written in key/value pairs.
2.  Keys must be strings, and values must be a valid JSON data type (string, number, 
    object, array, boolean or null).Keys and values are separated by a colon.
    Each key/value pair is separated by a comma.
3.  Syntax:
         { "name":"Somya", "age":25, "car":null }
4.  JSON objects are surrounded by curly braces {} 
    Tip to remember : Here, order of element is not important. That means you can go 
    the way you like. Therefore the shape of the braces i.e. wavy. 
    (Note : It is just my logic to remember the shape of both.)
5.  Order of elements is not important. 
    Example:  { rollno: 1, firstname: 'Somya'} 
                   is equal to 
             { firstname: 'Somya', rollno: 1}
6.  JSON can store nested objects in JSON format in addition to nested arrays.
SAM
  • 171
  • 1
  • 10
  • Under #5 in JSON array, shouldn't that example be in square brackets? – Mike Maxwell Apr 25 '19 at 20:20
  • That example was just to explain the concept. Though I have changed the example for better understanding. Thanks for asking @MikeMaxwell, questions like your improves our explanation skills. – SAM Apr 26 '19 at 05:32
5

The usage of both can be depended on the structure of your data.

Simply, You can use the Nested Objects approach if you plan to give priority to a unique identifier such as a Primary Key.

eg:

  {
     "Employees" : {
           "001" : {
               "Name" : "Alan",
               "Children" : ["Walker", "Dua", "Lipa"]
                },
           "002" : {
               "Name" : "Ezio",
               "Children" : ["Kenvey", "Connor", "Edward"]
                }
      }

Or, Use the Array first approach if you intend to store a set of values with no need to identify uniquely.

eg:

     {
        "Employees":[
               {
                   "Name" : "Alan",
                   "Children" : ["Walker", "Dua", "Lipa"]
               },
               {
                   "Name" : "Ezio",
                   "Children" : ["Kenvey", "Connor", "Edward"]
               }
          ]
       }
   

Although you could use the second method with an identifier, it can be harder or too complex to query and understand in some scenarios. Also depending on the database one may have to apply a suitable approach. Eg: MongoDB / Firebase

NeWi-SL
  • 127
  • 1
  • 6
3

When a JSON starts with {} it is an Object JSON object, and when it starts with [] it is an Array JSON Array.

A JSON array can consist of many objects and that is called an array of objects.

Danon
  • 2,771
  • 27
  • 37
0

I know, all of the previous answers are insightful to your question. I had too like you this confusion just one minute before finding this SO thread. After reading some of the answers, here is what I get: A JSONObject is a JSON-like object that can be represented as an element in the array, the JSONArray. In other words, a JSONArray can contain a (or many) JSONObject.

Serge Kishiko
  • 466
  • 1
  • 6
  • 13
0

Arrays in JSON are used to organize a collection of related items (Which could be JSON objects). For example: [{"name":"Name 1"},{"name": "Name 2} ]

On the other hand, you would use JSONObject when dealing with JSON that begins with curly braces.

A JSON object is typically used to contain key/value pairs related to one item. For example: {"name": "Name", "description":"a JSON object"}

Of course, JSON arrays and objects may be nested inside one another. One common example of this is an API that returns a JSON object containing some metadata alongside an array of the items matching your query:

{"startIndex": 0, "data": [{"name":"Name 1"},{"name": "Name 2"} ]} 
Procrastinator
  • 2,526
  • 30
  • 27
  • 36