Ir para o conteúdo

Lição 7 · Data and Expressions

Expressions in n8n

Write n8n expressions with $json, references to earlier nodes, built-in variables like $now, text and array methods, Luxon dates and safe fallbacks for missing data.

An expression is a small piece of JavaScript inside a node parameter. It lets a setting change for each item: a recipient, a subject line, a URL or a condition. Switch a field to expression mode, and everything between {{ }} is evaluated while the rest stays plain text.

The basics

TEXT
Order {{ $json.orderId }} totals {{ $json.total.toFixed(2) }} EUR.

$json is shorthand for the current item's data. The editor shows a preview of the result, so check it as you type. If you see undefined, the path is probably wrong or the data is missing.

Reaching earlier nodes

$json only sees the immediate input. To read an earlier node, name it:

TEXT
{{ $('Get customer').item.json.email }}
{{ $('Get customer').first().json.email }}
{{ $('Get orders').all().length }}
  • .item returns the item in that node that links to your current item.
  • .first() and .last() take one end of that node's output.
  • .all() returns every item.

If n8n cannot tell which item links to yours, .item fails. Then use .first(), .last() or pick by position from .all(), but only if you know the order is right. Renaming a node updates many references, but check expressions after large edits, especially inside code.

Useful built-ins

Variable What it gives
$json Current item data
$input The node's input, such as $input.all()
$('Name') Output of a named node
$now, $today Current time and start of today
$workflow Workflow information, such as its name
$execution Current execution information

The official expression reference lists many more.

Dates with Luxon

n8n uses the Luxon library for dates. $now is a Luxon DateTime in the workflow's time zone.

TEXT
{{ $now.toFormat('yyyy-MM-dd') }}
{{ $now.minus({ days: 7 }).toISODate() }}
{{ DateTime.fromISO($json.createdAt).toFormat('dd LLL yyyy') }}
{{ $now.diff(DateTime.fromISO($json.createdAt), 'days').days }}

Plain JavaScript Date objects and Luxon dates are not the same, so convert before mixing them. When you send dates to another system, prefer ISO format such as 2026-12-01T09:30:00+01:00, which carries the time zone.

Transforming values

Because expressions are JavaScript, you can use string, number and array methods:

TEXT
{{ $json.email.trim().toLowerCase() }}
{{ $json.name.split(' ')[0] }}
{{ $json.items.map(i => i.price).reduce((a, b) => a + b, 0) }}
{{ $json.score >= 70 ? 'pass' : 'review' }}

n8n also adds helper methods to some data types. Check the reference before relying on one.

Try the logic in plain JavaScript

You can practice the JavaScript outside n8n. The stand-in below defines a minimal $json and evaluates the same kinds of formulas.

JS Stand-in: practice expression logic

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A tiny template engine

This second stand-in shows how {{ }} templates fill in values per item. It handles only simple dotted paths, on purpose.

Python Stand-in: fill a template for every item

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second line shows (missing). In a real workflow, you decide what that should mean: skip the item, use a default or send it to review.

When to use a node instead

Short expressions are great. If an expression grows into several conditions and loops, move it to an Edit Fields step with several clear fields, or into the Code node. Future you will thank you.

Common mistakes

  • Forgetting to switch the field to expression mode, so braces are sent as plain text.
  • Using $json to reach a value that was in an earlier node and is no longer in the input.
  • Relying on .first() in a multi-item workflow, which gives every item the same first value.

Debugging an expression

Break a long expression into pieces. Type only {{ $json }} and check the preview to see the whole item. Then add one level at a time, such as {{ $json.customer }} and {{ $json.customer.email }}. The step where the preview turns empty is where your assumption is wrong. The preview relies on data from earlier runs, so run the earlier nodes first. If you see [object Object] inside text, you inserted a whole object. Choose a specific property, or convert the object with JSON.stringify() if you really want it as text.

When you handle money, be careful with decimals. JavaScript shows 0.1 + 0.2 as 0.30000000000000004. Work in the smallest unit, such as cents, or round where you display the value, and let your accounting system keep the authoritative totals.

Practice

Using the sample objects from the first stand-in, write expressions for these tasks: the customer's initials in capitals, the number of items, the total with two decimals and a label that says unknown city when no address exists. Try each one in the stand-in by replacing the printed lines, then predict what happens when name is empty. Fix any expression that crashes or shows undefined. Finally, write the same date as yesterday using $now and Luxon, and note which time zone it uses. Checking edge values now is much cheaper than finding them after publishing.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What does $('Get customer').first() return?

    It takes the first item of the named node's output.
  2. 2. Which library does n8n use for dates?

    n8n uses Luxon, and $now is a Luxon DateTime.
  3. 3. A node fails to resolve .item for an earlier node. What is a likely cause?

    Without a clear item link, .item is ambiguous.

Summary

  • Expressions in {{ }} compute values per item.
  • $json reads the current item; $('Node') reads earlier nodes.
  • $now and Luxon handle dates and time zones.
  • Add fallbacks for missing data and move complex logic into nodes or code.

Next: Transforming Data

Seu progresso fica salvo neste navegador. Não precisa de conta.