Working with
Nested JSON
Learn to navigate, access, and manipulate deeply nested objects and arrays in JavaScript using dot notation, bracket notation, and powerful iteration patterns.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data format used to store and exchange structured information. It closely mirrors JavaScript object syntax, making it the lingua franca of web APIs and configuration files.
What Makes JSON Nested?
Nesting means placing objects inside objects, arrays inside arrays, or any combination. Each value can itself be a complex structure, creating a tree of data with multiple levels of depth.
Why Nested JSON Matters
Almost every modern web application consumes data from APIs that return nested JSON. Understanding how to traverse, extract, and transform this data is a foundational developer skill.
The Two Access Notations
JavaScript gives you two ways to dig into nested structures: dot notation and bracket notation. Both can be chained to reach any depth.
Core Rule: Chaining Access
To reach a value at depth \( n \), chain exactly \( n \) access operations. Each step resolves one level of nesting before the next step runs.
Dot vs Bracket: When to Use Which
| Dot Notation | Bracket Notation | |
|---|---|---|
| Syntax | obj.key | obj["key"] |
| Dynamic Keys | ✕ No | ✓ Yes |
| Special Characters | ✕ No | ✓ Yes |
| Numeric Keys | ✕ No | ✓ Yes |
| Readability | ✓ High | Moderate |
| Best For | Static, known keys | Variables, loops, odd keys |
How to Navigate Nested JSON
Example 1: Basic Nesting
Example 2: Arrays Inside Objects
Example 3: Deep API Data
Iterating Nested Arrays
When an object contains an array, use .forEach(), .map(), or for...of to process each element. Combine iteration with property access to extract what you need.
Safe Access with Optional Chaining
If any intermediate level is undefined or null, the chain throws a TypeError. The ?. operator short-circuits and returns undefined instead of crashing.
Destructuring Nested Structures
Destructuring lets you unpack values from nested objects and arrays into named variables in a single statement, reducing repetitive access chains.
Common Mistakes
Technique Comparison
| Technique | Best For | Handles Missing? |
|---|---|---|
| Dot Notation | Static, known keys | ✕ No |
| Bracket Notation | Dynamic keys, loops | ✕ No |
| Optional Chaining | Uncertain data shapes | ✓ Yes |
| Destructuring | Extracting multiple values | Partial (with defaults) |
| .map() / .forEach() | Processing nested arrays | ✕ No |
Question 1 of 5
const data = { store: { items: [{ name: "Laptop", price: 999 }, { name: "Phone", price: 699 }] } };
Which expression correctly returns "Phone"?
Question 2 of 5
What does user?.address?.zip return if user.address is undefined?
Question 3 of 5
const team = { members: [{ name: "Ava", skills: ["JS", "Python"] }, { name: "Leo", skills: ["Rust", "Go"] }] };
Which expression returns "Python"?
Question 4 of 5
Given const obj = { a: { b: { c: 42 } } };, which destructuring correctly extracts c?
Question 5 of 5
const config = { db: { host: "localhost", ports: [5432, 5433] }, cache: null };
What does config.cache?.ttl ?? 300 evaluate to?