Nested JSON
Nested JSON means placing a JSON object or array inside another JSON object or array. This creates a multi-level structure, similar to folders inside folders on a computer.
Real-world data is rarely simple. A student doesn't just have a name — they also have an address, a list of subjects, contact details, and more. Nested JSON allows all of this complex information to be represented in one clean, organized structure.
Why Use Nested JSON?
- To represent hierarchical data (data with parent-child relationships)
- To keep related information grouped together
- To avoid repeating the same structure across multiple places
- To match how real-world entities are structured (e.g., an order contains a customer, which contains an address)
Nested Object Inside an Object
A JSON object can have another object as one of its values. Here is an example of a person with a nested address object:
{
"name": "Sanjay Mehta",
"age": 35,
"address": {
"street": "12, MG Road",
"city": "Ahmedabad",
"state": "Gujarat",
"pincode": "380001"
}
}The value of "address" is not a simple string or number — it is a full JSON object with its own keys and values.
Accessing Nested Object Values in JavaScript
To access data inside a nested object, chain the keys using dot notation:
const person = {
"name": "Sanjay Mehta",
"address": {
"city": "Ahmedabad",
"pincode": "380001"
}
};
console.log(person.address.city); // Output: Ahmedabad
console.log(person.address.pincode); // Output: 380001Nested Array Inside an Object
An object can also have an array as a value. Here, a student has a list of subjects:
{
"name": "Priya Sharma",
"class": "10th",
"subjects": ["Mathematics", "Science", "English", "Hindi"]
}Accessing the second subject:
console.log(student.subjects[1]); // Output: ScienceNested Objects Inside an Array
This is one of the most common patterns in real-world JSON — an array where each element is an object. It is used to represent a collection of records, like a list of employees:
{
"company": "TechSoft Solutions",
"employees": [
{
"id": 1,
"name": "Nikhil Joshi",
"role": "Developer"
},
{
"id": 2,
"name": "Sneha Kapoor",
"role": "Designer"
},
{
"id": 3,
"name": "Arjun Das",
"role": "Tester"
}
]
}Accessing the role of the second employee:
console.log(data.employees[1].role); // Output: DesignerDeeply Nested JSON (Multiple Levels)
JSON can be nested multiple levels deep. Here is a complex example representing an order in an e-commerce system:
{
"orderId": "ORD-5021",
"customer": {
"name": "Reena Verma",
"contact": {
"phone": "9876543210",
"email": "reena@example.com"
}
},
"items": [
{
"productName": "Running Shoes",
"quantity": 1,
"price": 1800
},
{
"productName": "Sports Socks",
"quantity": 3,
"price": 150
}
],
"paymentStatus": "Paid"
}Accessing the email of the customer:
console.log(data.customer.contact.email); // Output: reena@example.comAccessing the price of the second item:
console.log(data.items[1].price); // Output: 150Looping Through Nested Arrays of Objects
To process each employee in a list, use forEach:
const data = {
"employees": [
{ "name": "Nikhil", "role": "Developer" },
{ "name": "Sneha", "role": "Designer" },
{ "name": "Arjun", "role": "Tester" }
]
};
data.employees.forEach(function(emp) {
console.log(emp.name + " - " + emp.role);
});
// Output:
// Nikhil - Developer
// Sneha - Designer
// Arjun - TesterNested Array Inside an Array
Arrays can also be nested inside arrays, creating a grid or matrix structure:
{
"weeklySchedule": [
["Math", "English", "Science"],
["Hindi", "Art", "Computer"],
["Physics", "Chemistry", "PE"]
]
}Accessing "Art" (row 1, column 1):
console.log(data.weeklySchedule[1][1]); // Output: ArtVisual Structure of Nested JSON
Root Object
├── Simple Key: Value
├── Nested Object Key
│ ├── Key: Value
│ └── Key: Value
└── Array Key
├── Object [0]
│ ├── Key: Value
│ └── Key: Value
└── Object [1]
├── Key: Value
└── Key: ValueKey Points to Remember
- Nested JSON means objects or arrays inside other objects or arrays
- Use dot notation to access nested object values:
obj.level1.level2 - Use index for arrays:
obj.array[0].key - Nesting can go as deep as needed, but keeping it manageable improves readability
- Arrays of objects are the most common nesting pattern in real-world APIs
Summary
Nested JSON is the key to representing complex, real-world data structures. By placing objects inside objects, or arrays inside objects, JSON can model anything from a simple user profile to a full e-commerce order with multiple items, addresses, and payment details. Learning to read and write nested JSON is essential for any developer working with APIs or modern applications.
