Banner image with the title "JSON Accessing Nested Values" in blue uppercase text on a light grey gradient background

Accessing Nested Values in JSON – A Beginner’s Guide

Introduction

JSON (JavaScript Object Notation) is a popular format for storing structured data. While accessing top-level values is fairly straightforward, working with nested data can be a bit more complex, especially for beginners.

In this guide, we’ll focus on how to access nested values in JSON using simple expressions. You’ll learn how to navigate deeper levels of data using dot notation, with practical examples to help you along the way.

What Is a Nested Value?

A nested value refers to any value that is stored within another object or array in a JSON structure. In other words, the data is not at the top level but is inside one or more other objects.

Let’s start with an example:

{
  "user": {
    "name": "Alice",
    "profile": {
      "email": "[email protected]",
      "location": {
        "city": "Amsterdam",
        "country": "Netherlands"
      }
    }
  }
}

In this JSON structure:

  • user.name is nested one level deep
  • user.profile.email is two levels deep
  • user.profile.location.city is three levels deep

Accessing Nested Objects

To access nested objects, you use dot notation. Here’s how it works in practice:

{{ $json.user.name }}           → "Alice"
{{ $json.user.profile.email }}  → "[email protected]"
{{ $json.user.profile.location.city }} → "Amsterdam"

Each dot (.) moves you one level deeper into the structure.

Why Nested Data Exists

Nested data is used to organize related information logically. For example:

  • A user object may contain a profile, which in turn contains contact info
  • A product might contain specifications, prices, and availability per region
  • A task might contain metadata, dependencies, and logs

Using nesting keeps the data clean, grouped, and scalable.

Example: Accessing Nested Values in n8n

In a tool like n8n, when working with JSON from an API or a previous node, you can access nested data using the same dot notation in expressions:

{{ $json.customer.details.phone }}

This retrieves the phone number from within the details object of a customer.

Always ensure you reference the structure exactly as it appears — any typo or mismatch in the nesting path will result in undefined.

Handling Arrays Inside Nested Objects

What if the nested value is inside an array? Consider this JSON:

{
  "project": {
    "members": [
      { "name": "Alice", "role": "Manager" },
      { "name": "Bob", "role": "Developer" }
    ]
  }
}

You can access a specific member’s role like this:

{{ $json.project.members[1].role }}  → "Developer"

Arrays require indexing (starting at 0), and you can combine this with dot notation for precise access.

Common Mistakes to Avoid

  • Incorrect casing: JSON keys are case-sensitive. user.name is not the same as User.Name.
  • Forgetting brackets in arrays: Always use [index] for array access.
  • Assuming a key exists: If a key is missing or null, the expression will return undefined.

Conclusion

Accessing nested values in JSON is an essential skill when working with APIs, automation tools like n8n, or structured data in general. With just dot notation and a basic understanding of the structure, you can retrieve the exact value you need — no matter how deep it’s buried.

Once you’re comfortable with accessing nested objects and arrays, you’ll be better equipped to build powerful automations and integrations that rely on structured data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top