8.1 KiB
n8n Expression Examples
Real working examples from n8n workflows.
Example 1: Webhook Form Submission
Scenario: Form submission webhook posts to Slack
Workflow: Webhook → Slack
Webhook Input (POST):
{
"name": "John Doe",
"email": "john@example.com",
"company": "Acme Corp",
"message": "Interested in your product"
}
Webhook Node Output:
{
"headers": {"content-type": "application/json"},
"params": {},
"query": {},
"body": {
"name": "John Doe",
"email": "john@example.com",
"company": "Acme Corp",
"message": "Interested in your product"
}
}
In Slack Node (text field):
New form submission! 📝
Name: {{$json.body.name}}
Email: {{$json.body.email}}
Company: {{$json.body.company}}
Message: {{$json.body.message}}
Output:
New form submission! 📝
Name: John Doe
Email: john@example.com
Company: Acme Corp
Message: Interested in your product
Example 2: HTTP API to Database
Scenario: Fetch user data from API and insert into database
Workflow: Schedule → HTTP Request → Postgres
HTTP Request Returns:
{
"data": {
"users": [
{
"id": 123,
"name": "Alice Smith",
"email": "alice@example.com",
"role": "admin"
}
]
}
}
In Postgres Node (INSERT statement):
INSERT INTO users (user_id, name, email, role, synced_at)
VALUES (
{{$json.data.users[0].id}},
'{{$json.data.users[0].name}}',
'{{$json.data.users[0].email}}',
'{{$json.data.users[0].role}}',
'{{$now.toFormat('yyyy-MM-dd HH:mm:ss')}}'
)
Result: User inserted with current timestamp
Example 3: Multi-Node Data Flow
Scenario: Webhook → HTTP Request → Email
Workflow Structure:
- Webhook receives order ID
- HTTP Request fetches order details
- Email sends confirmation
Node 1: Webhook
Receives:
{
"body": {
"order_id": "ORD-12345"
}
}
Node 2: HTTP Request
URL field:
https://api.example.com/orders/{{$json.body.order_id}}
Returns:
{
"order": {
"id": "ORD-12345",
"customer": "Bob Jones",
"total": 99.99,
"items": ["Widget", "Gadget"]
}
}
Node 3: Email
Subject:
Order {{$node["Webhook"].json.body.order_id}} Confirmed
Body:
Dear {{$node["HTTP Request"].json.order.customer}},
Your order {{$node["Webhook"].json.body.order_id}} has been confirmed!
Total: ${{$node["HTTP Request"].json.order.total}}
Items: {{$node["HTTP Request"].json.order.items.join(', ')}}
Thank you for your purchase!
Email Result:
Subject: Order ORD-12345 Confirmed
Dear Bob Jones,
Your order ORD-12345 has been confirmed!
Total: $99.99
Items: Widget, Gadget
Thank you for your purchase!
Example 4: Date Formatting
Scenario: Various date format outputs
Current Time: 2025-10-20 14:30:45
ISO Format
{{$now.toISO()}}
Output: 2025-10-20T14:30:45.000Z
Custom Date Format
{{$now.toFormat('yyyy-MM-dd')}}
Output: 2025-10-20
Time Only
{{$now.toFormat('HH:mm:ss')}}
Output: 14:30:45
Full Readable Format
{{$now.toFormat('MMMM dd, yyyy')}}
Output: October 20, 2025
Date Math - Future
{{$now.plus({days: 7}).toFormat('yyyy-MM-dd')}}
Output: 2025-10-27
Date Math - Past
{{$now.minus({hours: 24}).toFormat('yyyy-MM-dd HH:mm')}}
Output: 2025-10-19 14:30
Example 5: Array Operations
Data:
{
"users": [
{"name": "Alice", "email": "alice@example.com"},
{"name": "Bob", "email": "bob@example.com"},
{"name": "Charlie", "email": "charlie@example.com"}
]
}
First User
{{$json.users[0].name}}
Output: Alice
Last User
{{$json.users[$json.users.length - 1].name}}
Output: Charlie
All Emails (Join)
{{$json.users.map(u => u.email).join(', ')}}
Output: alice@example.com, bob@example.com, charlie@example.com
Array Length
{{$json.users.length}}
Output: 3
Example 6: Conditional Logic
Data:
{
"order": {
"status": "completed",
"total": 150
}
}
Ternary Operator
{{$json.order.status === 'completed' ? 'Order Complete ✓' : 'Pending...'}}
Output: Order Complete ✓
Default Values
{{$json.order.notes || 'No notes provided'}}
Output: No notes provided (if notes field doesn't exist)
Multiple Conditions
{{$json.order.total > 100 ? 'Premium Customer' : 'Standard Customer'}}
Output: Premium Customer
Example 7: String Manipulation
Data:
{
"user": {
"email": "JOHN@EXAMPLE.COM",
"message": " Hello World "
}
}
Lowercase
{{$json.user.email.toLowerCase()}}
Output: john@example.com
Uppercase
{{$json.user.message.toUpperCase()}}
Output: HELLO WORLD
Trim
{{$json.user.message.trim()}}
Output: Hello World
Substring
{{$json.user.email.substring(0, 4)}}
Output: JOHN
Replace
{{$json.user.message.replace('World', 'n8n')}}
Output: Hello n8n
Example 8: Fields with Spaces
Data:
{
"user data": {
"first name": "Jane",
"last name": "Doe",
"phone number": "+1234567890"
}
}
Bracket Notation
{{$json['user data']['first name']}}
Output: Jane
Combined
{{$json['user data']['first name']}} {{$json['user data']['last name']}}
Output: Jane Doe
Nested Spaces
Contact: {{$json['user data']['phone number']}}
Output: Contact: +1234567890
Example 9: Code Node (Direct Access)
Code Node: Transform webhook data
Input (from Webhook node):
{
"body": {
"items": ["apple", "banana", "cherry"]
}
}
Code (JavaScript):
// ✅ Direct access (no {{ }})
const items = $json.body.items;
// Transform to uppercase
const uppercased = items.map(item => item.toUpperCase());
// Return in n8n format
return [{
json: {
original: items,
transformed: uppercased,
count: items.length
}
}];
Output:
{
"original": ["apple", "banana", "cherry"],
"transformed": ["APPLE", "BANANA", "CHERRY"],
"count": 3
}
Example 10: Environment Variables
Setup: Environment variable API_KEY=secret123
In HTTP Request (Headers)
Authorization: Bearer {{$env.API_KEY}}
Result: Authorization: Bearer secret123
In URL
https://api.example.com/data?key={{$env.API_KEY}}
Result: https://api.example.com/data?key=secret123
Template from Real Workflow
Based on n8n template #2947 (Weather to Slack)
Workflow Structure
Webhook → OpenStreetMap API → Weather API → Slack
Webhook Slash Command
Input: /weather London
Webhook receives:
{
"body": {
"text": "London"
}
}
OpenStreetMap API
URL:
https://nominatim.openstreetmap.org/search?q={{$json.body.text}}&format=json
Weather API (NWS)
URL:
https://api.weather.gov/points/{{$node["OpenStreetMap"].json[0].lat}},{{$node["OpenStreetMap"].json[0].lon}}
Slack Message
Weather for {{$json.body.text}}:
Temperature: {{$node["Weather API"].json.properties.temperature.value}}°C
Conditions: {{$node["Weather API"].json.properties.shortForecast}}
Summary
Key Patterns:
- Webhook data is under
.body - Use
{{}}for expressions (except Code nodes) - Reference other nodes with
$node["Node Name"].json - Use brackets for field names with spaces
- Node names are case-sensitive
Most Common Uses:
{{$json.body.field}}- Webhook data{{$node["Name"].json.field}}- Other node data{{$now.toFormat('yyyy-MM-dd')}}- Timestamps{{$json.array[0].field}}- Array access{{$json.field || 'default'}}- Default values
Related: See COMMON_MISTAKES.md for error examples and fixes.