JSON Nested Objects
When we are writing the JSON data, we have some key-value pairs. We are now already familiar with the fact, that the keys are supposed to be in the double quotes, and the values associated with the respective keys are supposed to belong to some valid data. So, the thing is that the value here can be some object as well. So at times, when it is required that the value should be an entire object, we can have it. By this, we simply mean that the objects can be nested within the objects here.
So now, we are going to have a look at how can we have the value associated with the key as an object. We will first have a look at an actual example, where the value for some key is an object, and then we would try to explain the things. Let’s have a look at an example now.
{
“productName”:”GyaniPandit Mobile Phone”,
“price”:14500,
“emailAddress”:”[email protected]”,
“isGift”: false,
“shippingAddress”:{
“home”:”Here is some address…”,
“City”:”Mumbai”,
“State”:”Maharashtra”
}
}
As you can see in the above example, the data seems to be about some product order details. There are some easily understandable data, like the name of the product, the price of the product, the email address of the purchaser, a boolean data specifying whether this order should be treated as a gift, or general order, and the shipping address. Basically, this is some dummy data that we have taken for our understanding. For the key “shippingAddress”, you can simply see that the associated value is an object, which again has some key-value pairs, which are essentially some details related to the shipping address for the package.
As you can see here as well, to create the object, we have made use of the curly braces, and it contains some key-value pairs.
Since we are having multiple key-value pairs here, we are separating them with commas. Also, you can see that we have created an object at the top level. So, this is how we can simply create an object inside an object.