JSON Array of an Object

JSON Array Object

We are quite familiar with the arrays here. We have seen that the array is simply some collection of data, and that data can belong to some valid type in JSON. So, here, at times, when we are required to have an array, which contains some objects, we can have it in our JSON file. The name itself can help you visualize, that we are going to have an array of objects. So, the elements of the array are going to be objects.

Let’s have a look at some examples, through which, we can simply understand the concept of having objects within the array. For the example, we are going to consider an array of different products, and every product will be an object, with further key – pairs inside it.

{
“customerName”:”Kevin”,
“itemsPurchased”:
[
{
“productName”:”Tshirt”,
“brand”:”GyaniPandit Clothing”,
“price”:499
},
{
“productName”:”Jeans”,
“brand”:”GyaniPandit JeansHouse”,
“price”:999
},
{
“productName”:”Transperant Goggles”,
“brand”:”GyaniPandit eyewears”,
“price”:259
}

]
}

As you can see in the above example, some details are related to some of the items purchased by a person named Kevin.

There is a key, named “itemsPurchased”, the whose associated value is an array, of objects. These objects are simply some items, that the person has purchased. So, we have three objects within the array. Also, you can see that the objects have some more key-value pairs, containing different information related to that particular item.

If we have a look at some individual object inside that array, it looks something like this –
{
“productName”:”Transperant Goggles”,
“brand”:”GyaniPandit eyewears”,
“price”:259
}

As you can see, this is an individual object from the array. So, the thing is that the object contains some other key-value pairs, which are some information related to the item purchased by the user. Remember that we have made use of square brackets for an array, and curly braces for an object.