No history yet

Advanced Expression Mapping

Beyond Adjacent Nodes

You're likely used to dragging and dropping variables from the node immediately preceding the one you're working on. This is great for simple, linear workflows. But what happens when you need data from a node several steps back? Instead of adding extra 'Set' nodes to carry data along, you can access it directly.

n8n allows you to reference any node in your workflow by its name using the $node object. This gives you a direct line to the data you need, no matter where it is.

The syntax is $node["Node Name"].json.propertyName.

Simply replace 'Node Name' with the exact name of the node you want to pull from. If the node name has spaces, you must use quotes around it. This lets you skip over intermediate steps and keep your workflow clean.

Mastering Time with Luxon

Dealing with dates and times can be tricky. n8n simplifies this by bundling the powerful Luxon library, which you can use directly within your expressions. This lets you parse, manipulate, and format dates on the fly without needing a dedicated 'Date & Time' node.

To use it, you start with your date variable and chain Luxon methods to it. First, you almost always need to convert the date string into a DateTime object that Luxon can understand. The .toDateTime() method handles this.

// Let's say $json.createdAt is "2023-10-27T10:00:00.000Z"

// Add 7 days to the creation date and format it for a report
{{$json.createdAt.toDateTime().plus({days: 7}).format('yyyy-MM-dd')}}

// Result: "2023-11-03"

The .plus() method takes an object specifying the duration to add, like {days: 7} or {hours: 3}. The .format() method uses tokens to define the output string. For example, dd LLLL yyyy would produce something like '27 October 2023'.

Inline Logic and Item Linking

Sometimes you need to apply simple conditional logic. Instead of adding an 'IF' node that splits your workflow, you can use a ternary operator directly inside an expression. This is a compact way to write an if-then-else statement.

The syntax is condition ? value_if_true : value_if_false.

For example, you could check if a customer's order value is over $100 to assign them 'VIP' status.

{{$json.orderValue > 100 ? 'VIP' : 'Standard'}}

This is powerful, but what about matching items from different data streams? When you merge two branches, n8n needs to know which item from stream A corresponds to which item from stream B. By default, it pairs them by their index (the first with the first, second with second, etc.).

You can gain more control by using the $item() expression. This allows you to specify exactly which item you want to retrieve from another node's output, based on an index from your current node's loop.

$item(executionIndex).json.property\text{{\color{gray}{\$item(executionIndex).json.property}}}

This technique is crucial for ensuring data integrity when your workflow branches and then merges again, especially if the branches produce lists of different lengths or orders. It tells n8n how to correctly pair up related pieces of information.

Time to test your knowledge of these advanced techniques.

Quiz Questions 1/5

Your workflow has a node named 'Fetch Customer List' at the very beginning. You are now working in a node much later in the flow. How do you correctly reference the email address from the first item of that initial node?

Quiz Questions 2/5

You have an incoming date string, 2023-10-27, and you need to add 3 months to it using a Luxon expression. Which expression accomplishes this?

With these expression skills, you can build more efficient and readable workflows by handling complex data transformations without adding unnecessary nodes.