What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data format used for storing and exchanging data between systems. Think of it as a universal language for data.
JSON vs Other Formats
✅ Lightweight
✅ Human-readable
✅ Native JS support
❌ Verbose
✅ Schema validation
❌ Harder to parse
✅ Simple
❌ No nested data
❌ No data types
JSON Structure Breakdown
{
"name": "John Doe",
"age": 30,
"isActive": true,
"salary": null,
"skills": [
"JavaScript",
"Python"
],
"address": {
"street": "123 Main St",
"city": "New York"
}
}
Data Types Explained
| Type | Example | Rules & Notes |
|---|---|---|
| String | "Hello World" |
Must use double quotes, supports escape characters (\n, \t, \") |
| Number | 42, -3.14, 1.2e5 |
No quotes, supports integers, decimals, scientific notation |
| Boolean | true, false |
Lowercase only, represents true/false conditions |
| Null | null |
Represents absence of value (not 0 or empty string) |
| Array | [1, "text", true] |
Ordered list, can contain any data types |
| Object | {"key": "value"} |
Key-value pairs, keys must be strings |
Real-World JSON Examples
{
"userId": 12345,
"username": "john_doe",
"profile": {
"firstName": "John",
"lastName": "Doe",
"email": "[email protected]",
"preferences": {
"theme": "dark",
"notifications": true
}
},
"roles": ["user", "admin"],
"lastLogin": "2024-01-15T10:30:00Z"
}
{
"status": "success",
"data": [
{
"id": 1,
"title": "Learn n8n",
"completed": false,
"createdAt": "2024-01-01T00:00:00Z"
}
],
"meta": {
"total": 1,
"page": 1,
"limit": 10
}
}
JSON & Expressions in n8n
| Access Pattern | Example | Use Case |
|---|---|---|
| Simple field | {{ $json.name }} |
Get top-level field |
| Nested object | {{ $json.user.profile.email }} |
Access deeply nested data |
| Array element | {{ $json.items[0] }} |
Get first array item |
| Dynamic field | {{ $json["field-name"] }} |
Fields with special characters |
| All array items | {{ $json.items.map(item => item.name) }} |
Extract field from all items |
{{ $json.postContent }} accesses the "postContent"
field from the input data, which contains an array of social media
posts that can be processed by the AI model.
JSON Manipulation in n8n
// Convert object to JSON string
{{ $json.data.toJsonString() }}
// Parse JSON string to object
{{ JSON.parse($json.jsonString) }}
// Check if field exists
{{ $json.field !== undefined }}
// Get all object keys
{{ Object.keys($json) }}
// Get array length
{{ $json.items.length }}
// Map over array (use normal JavaScript)
{{ $json.items.map(item => item.name) }}
// Filter array
{{ $json.items.filter(item => item.active) }}
// Merge objects
{{ Object.assign({}, $json, {newField: "value"}) }}
Common JSON Issues & Solutions
- Single quotes:
'invalid' - Trailing commas:
{a: 1,} - Comments:
// not allowed - Undefined values
- Double quotes:
"valid" - No trailing commas
- No comments allowed
- Use
nullinstead