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?
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"} ]}
The difference is the same as a (Hash)Map vs List.
JSONObject:
{ID : 1}
{id: 1, name: 'B'}
is equal to {name: 'B', id: 1}
. JSONArray:
[1, 'value']
[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"}
]}
Best programmatically Understanding.
when syntax is
{}
then this isJsonObject
when syntax is
[]
then this isJsonArray
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 !
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]}
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.
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
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.
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.
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"} ]}