Introduction
In modern web development, APIs, and automation tools like n8n, JSON (JavaScript Object Notation) is a key data format used to structure data. Knowing how to work with JSON expressions is crucial for extracting the information you need efficiently.
In this blog, we will cover basic access methods for working with JSON data. This will provide you with the foundation for navigating and retrieving data in a straightforward way.
What Is a JSON Expression?
A JSON expression is a simple line of code that allows you to access specific parts of a JSON object or array. In tools like n8n, JSON expressions are wrapped in {{ }}
, which allows you to reference values dynamically.
For example:
{{ $json.name }}
This expression retrieves the value associated with the name
key in your JSON data.
Basic JSON Structure Example
Let’s look at a simple JSON example:
{
"user_name": "Alice",
"age": 30,
"is_active": true,
"skills": ["JavaScript", "Python", "n8n"]
}
1. Accessing Top-Level Keys
The first step is accessing top-level keys. These are the keys directly inside the main object.
To access basic information like user_name
or age
, use this format:
{{ $json.user_name }} → Alice
{{ $json.age }} → 30
{{ $json.is_active }} → true
In this case, we’re referencing:
$json.user_name
to get “Alice”$json.age
to get “30”$json.is_active
to gettrue
2. Accessing Array Values
Now, let’s look at arrays. In JSON, arrays are indexed, meaning that each item has a position (starting from 0). Here’s how to access items from the skills
array:
{{ $json.skills[0] }} → JavaScript
{{ $json.skills[1] }} → Python
{{ $json.skills[2] }} → n8n
Remember that array indices are zero-based!
3. Working with Nested Objects
If your JSON contains nested objects, you can access their properties by chaining keys. For example, consider the following JSON:
{
"user": {
"name": "Alice",
"profile": {
"email": "[email protected]"
}
}
}
To access the nested email
value:
{{ $json.user.profile.email }} → [email protected]
You simply use dot notation to access deeper levels of the object structure.
Why Basic Access Matters
Understanding how to access data using basic JSON expressions is crucial because it helps you:
- Retrieve specific data points easily
- Work with dynamic data in automation tools (like n8n)
- Simplify the process of referencing values without complex logic
Once you master basic access, you can build on these skills to handle more complex tasks such as filtering, transforming, or even looping over arrays.
Conclusion
Basic access to JSON data is a fundamental skill in modern development. Whether you’re working with APIs, automation workflows, or just manipulating JSON objects, mastering basic expressions will allow you to retrieve and display the exact data you need efficiently.