Perfect! Let’s do a deep dive into n8n Expressions—everything you need to know to master them. I’ll break it down step by step, from basics to advanced use cases.
1. What is an Expression in n8n?
An expression is a way to dynamically access, modify, or calculate data in a workflow.
- Anywhere you see a field with
{{}}, you can use an expression. - Expressions let you reference:
- Workflow data (
$json) - Node metadata (
$node) - Parameters (
$parameter) - Date & system info (
$now,$today)
- Workflow data (
Example:
{{$json["firstName"]}} {{$json["lastName"]}}
This will combine firstName and lastName from the current node’s data.
2. Basic Syntax
- Expressions are wrapped in double curly braces:
{{ ... }} - The expression itself is JavaScript, so you can use JS operators and functions.
Examples:
- Arithmetic:
{{ $json["price"] * 1.1 }}→ adds 10% tax - String concat:
{{ $json["firstName"] + " " + $json["lastName"] }} - Conditional:
{{ $json["age"] > 18 ? "Adult" : "Minor" }}
3. Accessing Data
a) Current Node Data
{{$json["fieldName"]}}
$jsonis the current node’s data.- Use dot or bracket notation:
{{$json.name}} {{$json["name"]}}
b) Previous Node Data
{{$node["Node Name"].json["field"]}}
- Access data from another node in the workflow.
- Example:
{{$node["HTTP Request"].json["id"]}}
c) All Items / Arrays
{{$json["items"][0]["price"]}}
- Useful for loops or arrays.
- n8n nodes often output arrays; remember to iterate if needed.
4. Workflow Metadata
$workflow→ Access workflow info, like ID or name.$now→ Current timestamp$today→ Current date
Example:
Order placed at {{$now}}
5. Conditional Expressions
- Use JavaScript ternary operator:
{{$json["status"] === "paid" ? "✅ Paid" : "❌ Pending"}}
- Works anywhere expressions are allowed (Set node, Function node, etc.).
6. Functions & Transformations
Since expressions are JS, you can use functions:
- String:
{{ $json["name"].toUpperCase() }}
- Date:
{{ new Date($json["createdAt"]).toLocaleDateString() }}
- Math:
{{ Math.round($json["price"] * 1.1) }}
- Arrays:
{{ $json["items"].map(item => item.name).join(", ") }}
7. Special Variables
| Variable | Use Case |
|---|---|
$json |
Current node’s data |
$node |
Another node’s data |
$parameter |
Node parameters |
$binary |
Access binary data |
$now |
Current timestamp |
$today |
Current date |
8. Examples in Real Workflows
Example 1: Combine Name
{{$json["firstName"] + " " + $json["lastName"]}}
Example 2: Calculate Discount Price
{{$json["price"] * (1 - $json["discount"]/100)}}
Example 3: Access Previous Node’s Result
{{$node["HTTP Request"].json["orderId"]}}
Example 4: Conditional Output
{{$json["stock"] > 0 ? "In Stock" : "Out of Stock"}}
Example 5: Loop Through Array
{{ $json["items"].map(i => i.name).join(", ") }}
9. Tips to Master Expressions
- Always test small expressions first in a Set or Function node.
- Use Execution Preview to see what
$jsoncontains. - Remember: everything is JavaScript, so JS knowledge is key.
- For arrays,
.map(),.filter(), and.reduce()are your friends. - For debugging, output complex expressions to a
Setnode and inspect results.
If you want, I can make a complete cheat sheet of n8n expressions including all the advanced tricks, like:
- Looping arrays in expressions
- Formatting dates
- Nested JSON access
- Error-safe expressions
It would be like a master key for every expression you need in n8n.
Do you want me to make that cheat sheet?
Perfect! Here’s a complete n8n Expressions Cheat Sheet, from beginner to advanced, including all the tricks you need to master workflow data.
🗝️ n8n Expressions Cheat Sheet
1. Basic Syntax
- All expressions are wrapped in
{{ }} - Expressions are JavaScript, so you can use JS operators, functions, and ternary logic.
{{ $json["fieldName"] }} // Access current node data
{{ $json.fieldName }} // Dot notation alternative
{{ $json["field"] + " text" }} // Concatenate strings
{{ $json["price"] * 1.1 }} // Math operations
2. Accessing Data
| Variable | Description |
|---|---|
$json |
Current node’s JSON output |
$node["Node"] |
Another node’s output |
$parameter |
Node parameters |
$binary |
Binary data |
$now |
Current timestamp |
$today |
Current date |
Examples:
{{$node["HTTP Request"].json["id"]}} // Access previous node
{{$parameter["limit"]}} // Node parameter
{{$now}} // Current time
{{$today}} // Current date
3. Strings
{{ $json["firstName"] + " " + $json["lastName"] }} // Concatenate
{{ $json["name"].toUpperCase() }} // Uppercase
{{ $json["email"].toLowerCase() }} // Lowercase
{{ `Hello, ${$json["firstName"]}` }} // Template literal
4. Numbers
{{ $json["price"] * 1.1 }} // Multiply
{{ Math.round($json["price"]) }} // Round
{{ Math.floor($json["price"]) }} // Floor
{{ Math.ceil($json["price"]) }} // Ceiling
{{ Math.random() }} // Random number
5. Conditional Logic
- Use JavaScript ternary operator
? :
{{ $json["status"] === "paid" ? "✅ Paid" : "❌ Pending" }}
{{ $json["stock"] > 0 ? "In Stock" : "Out of Stock" }}
6. Arrays & Loops
{{ $json["items"].map(i => i.name).join(", ") }} // Combine array items
{{ $json["numbers"].filter(n => n > 10) }} // Filter numbers
{{ $json["numbers"].reduce((a,b)=>a+b,0) }} // Sum array
Access first/last item:
{{ $json["items"][0] }} // First item
{{ $json["items"][$json["items"].length - 1] }} // Last item
7. Dates & Time
{{ new Date($json["createdAt"]).toLocaleDateString() }} // Format date
{{ new Date($json["createdAt"]).toISOString() }} // ISO string
{{ new Date().toLocaleTimeString() }} // Current time
{{ $now }} // Timestamp
{{ $today }} // Today’s date
8. Accessing Other Nodes
{{ $node["HTTP Request"].json["orderId"] }} // Single field
{{ $node["Previous Node"].json["items"][0]["price"] }} // Array element
{{ $node["Set Node"].json["fullName"] }} // From Set node
9. Nested JSON
{{ $json["customer"]["address"]["city"] }} // Nested object
{{ $json["orders"][0]["items"][1]["name"] }} // Array inside object
10. Error-Safe Expressions
- Prevent workflow crash if a field is missing:
{{ $json["field"] ? $json["field"] : "default" }}
{{ $json?.field ?? "default" }} // Optional chaining
11. Binary Data
{{ $binary["data"].data }} // Access binary data
{{ $binary["file"].fileName }} // Get file name
12. Advanced Tips & Tricks
- Map & Join:
{{ $json["items"].map(i => i.name.toUpperCase()).join(", ") }}
- Filter & Sum:
{{ $json["items"].filter(i => i.price>10).reduce((a,b)=>a+b.price,0) }}
- Conditional Arrays:
{{ $json["items"].map(i => i.stock>0 ? i.name : null).filter(Boolean) }}
- Use Template Literals for Clean Strings:
{{ `Order ${$json["orderId"]} placed by ${$json["customer"]["name"]}` }}
- Dynamic Node Reference:
{{ $node[$parameter["nodeName"]].json["field"] }}
✅ Summary
$json→ Current node$node→ Other node$parameter→ Node params$binary→ Binary data$now,$today→ Time & date- Expressions = JS + n8n context