n8n Mastery for Automation Architects
Advanced JavaScript Expressions
Beyond the Basics of Expressions
You already know how to pull data from previous nodes using n8n's expression syntax, {{ }}. Now, let's move beyond simple data retrieval and start transforming data directly inside your nodes. Mastering these techniques will make your workflows cleaner, more efficient, and far more powerful.
Navigating Complex Data
APIs often return data in deeply nested JSON objects, where the information you need is buried several layers deep. Instead of adding extra nodes to parse this, you can use dot notation to pinpoint the exact value you need. Each dot acts as a step deeper into the object's structure.
If a key contains spaces or special characters, you can use bracket notation instead. For example, to access a key named "customer-id", you would write $json["customer-id"]. You can even use expressions to create dynamic keys in your output. This is useful for organizing data based on input.
// Setting a dynamic key
{
"user_{{ $json.id }}": {
"name": "{{ $json.name }}",
"email": "{{ $json.email }}"
}
}
Inline Logic and Conditionals
Sometimes you need to make a quick decision based on incoming data. Instead of using an IF node for simple logic, you can use a directly within an expression. This lets you choose between two values based on a condition, all in one line.
The syntax is condition ? value_if_true : value_if_false.
Imagine an e-commerce workflow. You want to label an order as 'Urgent' if the total is over đź’˛100. Otherwise, it's 'Standard'.
{{ $json.orderTotal > 100 ? 'Urgent' : 'Standard' }}
This single expression replaces an entire IF node, keeping your workflow streamlined.
Mastering Dates and Times
Dealing with dates and times can be tricky. Fortunately, n8n has the powerful library built-in, accessible through the $today or $now objects. You can format dates, add or subtract time, and calculate durations without any extra setup.
Let's say you need to set a follow-up date for one week from today and format it for a database.
{{ $today.plus({ days: 7 }).toISODate() }}
This expression takes the current date, adds seven days, and then formats the result into the YYYY-MM-DD format. You can also parse incoming date strings and reformat them.
If you receive a date like "April 20, 2024", you can convert it to a standard format like this:
{{ DateTime.fromFormat($json.unformattedDate, 'MMMM d, yyyy').toISODate() }}
Transforming Arrays on the Fly
One of the most powerful uses of expressions is processing lists of items. Instead of using a Loop node, you can often use JavaScript's built-in array methods like .map() and .filter() to reshape data instantly.
The method is perfect for transforming each item in an array. Imagine you have a list of products from an API, but you only need their names and prices for a report.
{{ $json.products.map(item => ({ name: item.productName, price: item.cost })) }}
This one-liner iterates over the products array and creates a new array of objects with simplified keys. The item variable represents each product object as the expression loops through them.
Similarly, the .filter() method lets you selectively keep items that meet a certain condition. For example, to get a list of only the products that are in stock:
{{ $json.products.filter(item => item.stock > 0) }}
Combining these methods allows for incredibly complex data manipulation in a single expression. You can filter for items that are in stock and then map the results to extract just the product IDs.
{{ $json.products.filter(item => item.stock > 0).map(item => item.id) }}
By handling these transformations directly in your expressions, you build workflows that are not only more efficient but also easier to read and maintain.