How to access nested json objects in JavaScript?

Accessing nested JSON objects in JavaScript involves navigating through multiple levels of object properties. Nested objects are objects contained within other objects, creating a hierarchical data structure.

What are Nested JSON Objects?

A nested JSON object has one or more objects as property values. This creates multiple layers that you need to traverse to access specific data.

Using Dot Notation

The most common way to access nested properties is using dot notation, where you chain property names with dots.

Example 1: Single Level Nesting

<html>
<body>
<script>
   var person = {
       "name": "Ram",
       "age": 27,
       "vehicles": {
           "car": "limousine",
           "bike": "ktm-duke",
           "plane": "lufthansa"
       }
   }
   document.write("Mr Ram has a car called " + person.vehicles.car);
</script>
</body>
</html>
Mr Ram has a car called limousine

Example 2: Multiple Level Nesting

You can access deeply nested objects by chaining multiple dot notations together.

<html>
<body>
<script>
   var person = {
       "name": "Ram",
       "age": 27,
       "vehicles": {
           "car": "limousine",
           "bike": "ktm-duke",
           "airlines": {
               "lufthansa": "Air123",
               "British airways": "Brt707"
           }
       }
   }
   document.write("Mr Ram travels by plane called " + person.vehicles.airlines.lufthansa);
</script>
</body>
</html>
Mr Ram travels by plane called Air123

Using Bracket Notation

Bracket notation is useful when property names contain spaces or special characters.

<html>
<body>
<script>
   var person = {
       "name": "Ram",
       "vehicles": {
           "airlines": {
               "British airways": "Brt707"
           }
       }
   }
   document.write("British Airways flight: " + person.vehicles.airlines["British airways"]);
</script>
</body>
</html>
British Airways flight: Brt707

Safe Access with Optional Chaining

Modern JavaScript supports optional chaining (?.) to safely access nested properties without errors if intermediate properties are undefined.

<html>
<body>
<script>
   var person = {
       "name": "Ram"
   }
   // Safe access - won't throw error if vehicles doesn't exist
   var car = person.vehicles?.car ?? "No car available";
   document.write("Car: " + car);
</script>
</body>
</html>
Car: No car available

Key Points

  • Use dot notation for simple property names: object.property.subproperty
  • Use bracket notation for property names with spaces or special characters
  • Optional chaining (?.) prevents errors when accessing potentially undefined properties
  • Always validate nested object existence before accessing deep properties

Conclusion

Accessing nested JSON objects requires understanding dot notation and bracket notation. Use optional chaining for safe access to prevent runtime errors when dealing with uncertain data structures.

Updated on: 2026-03-15T23:18:59+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements