Mastering Advanced n8n Workflows
Complex Expressions Manipulation
Beyond Drag and Drop
Connecting nodes on the n8n canvas is just the beginning. The real power to create flexible, intelligent workflows lies in expressions. Expressions let you manipulate data directly inside a node's parameter fields, without needing a dedicated Code node for every small adjustment.
Any time you see a field that can be switched to 'Expression', you can unlock this capability. The key is the double curly brace syntax: {{ }}. Anything inside these braces is treated as JavaScript-like code, allowing you to dynamically access, transform, and format data as it flows through your workflow.
Think of expressions as small, powerful functions you can run inside any node parameter.
Accessing Your Data
Before you can transform data, you need to reference it. n8n provides a few special variables for this purpose. Let's say you have an HTTP Request node that fetches user data, and the output for a single item looks like this:
{
"id": 101,
"name": "Alex Ray",
"email": "alex.ray@example.com",
"created": "2023-10-27T10:00:00.000Z",
"plan": "premium"
}
Here’s how you would access different parts of this data in a subsequent node.
The $json variable
This is your primary tool. $json refers to the JSON object of the current item being processed. To get the user's name, your expression would be:
{{ $json.name }}
The $node variable
Sometimes, a node between your data source and your current node might change the data structure. The $node variable lets you reach back to the output of a specific previous node by its name. This ensures you get the original, unmodified data. If your HTTP Request node was named 'GetUser', you could access the email like this:
{{ $node["GetUser"].json.email }}
The $item variable
When you're processing items inside a loop or a split, $item refers to the single item in the current iteration. It's often used to keep track of which item you're working with in more complex scenarios. It provides the index and the data for that specific run:
{{ $item.json.plan }}
Transforming Data on the Fly
Once you can access data, you can start manipulating it. n8n's expression engine supports standard JavaScript methods for strings and numbers.
You can combine data from multiple fields, perform calculations, and reformat text with single-line expressions.
For example, you could create a welcome message by combining the user's name with a static string. The + operator works for string concatenation:
{{ "Welcome, " + $json.name + "!" }}
// Result: "Welcome, Alex Ray!"
You can also use common string methods to change the case or extract parts of a string.
{{ $json.email.toUpperCase() }}
// Result: "ALEX.RAY@EXAMPLE.COM"
{{ $json.name.slice(0, 4) }}
// Result: "Alex"
Mathematical operations work just as you'd expect. If you had a price and quantity field, you could calculate a total cost:
{{ $json.price * $json.quantity }}
Working with Dates
Date and time manipulation is a common requirement in automation. n8n includes the powerful Luxon library to make this easier. You can access it through the DateTime object within an expression.
To get the current date and time, use the special $now variable. $now is already a Luxon DateTime object, so you can call Luxon methods on it directly.
{{ $now.toISODate() }}
// Result: "2024-05-21" (for example)
{{ $now.plus({ days: 7 }).toFormat('DDDD') }}
// Result: "October 27, 2023" (for example)
You can also parse existing date strings from your data. Let's use the created date from our user example. First, we create a Luxon object from the ISO string, then we can format it however we like.
{{ DateTime.fromISO($json.created).toFormat('ff') }}
// Result: "October 27, 2023 at 10:00 AM UTC"
Conditional Logic in Expressions
What if you need to set a value based on a condition? You don't always need an IF node for simple logic. The ternary operator is a compact way to write an if-else statement inside an expression.
The syntax is:
condition ? value_if_true : value_if_false
Let's say we want to set a status based on whether the user has a premium plan. We can check the value of $json.plan and return a different string for each outcome.
{{ $json.plan === 'premium' ? 'VIP User' : 'Standard User' }}
// Result: "VIP User"
This single line does the work of an IF node, keeping your workflow cleaner and more efficient for simple conditional assignments. It's a fundamental technique for building responsive and intelligent automations.
What is the primary purpose of using expressions in n8n?
Which expression correctly retrieves the email value from a specific node named FetchUserData?
Mastering these expression techniques is a major step toward creating truly powerful n8n workflows.