DEV Community

Pirate Prentice
Pirate Prentice

Posted on

n8n Expressions: How to Use Dynamic Data in Your Workflows (Free Examples)

If you have ever copy-pasted the same value into a dozen nodes, you already know the pain. n8n expressions are the fix. They let you pull live data from any previous node, do math, format dates, and build strings in any field in your workflow.

This post covers everything you need to get moving: syntax, the most useful built-in variables, and real-world examples you can drop into your own workflows.


What Is an n8n Expression?

An expression is JavaScript wrapped in double curly braces.
Whenever n8n sees those braces in a field, it evaluates what is inside and uses the result.

Example: {{ $json.email }} grabs the email field from the current item. No node. No extra step. Just live data.


How to Open the Expression Editor

  1. Hover over any input field in a node.
  2. Click the = icon that appears on the right.
  3. The field turns purple, you are now in expression mode.
  4. Type your expression. The preview pane at the bottom shows the live result.

The Core Variables

$json - current item data

  • $json.name - simple field
  • $json.order.total - nested field
  • $json.items[0].sku - array element

$json is the data object of the current item being processed.

$('Node Name').item.json - data from a specific node

Pull data from any previous node by name. This is the most powerful pattern.

Example: $('HTTP Request').item.json.id or $('Get User').item.json.profile.avatar_url

$now - current timestamp (Luxon DateTime)

  • $now.toISO() - full ISO string
  • $now.toFormat('yyyy-MM-dd') - formatted date
  • $now.minus({ days: 7 }).toISO() - one week ago

$vars - workflow variables (n8n 1.x+)

Set static values once in Settings > Variables and reference them everywhere.

  • $vars.SLACK_CHANNEL
  • $vars.API_BASE_URL

$env - environment variables (self-hosted only)

  • $env.MY_SECRET_KEY

JavaScript Is Fair Game

Everything inside {{ }} is evaluated as JavaScript:

// String methods
{{ $json.name.toUpperCase() }}
{{ $json.email.split('@')[0] }}

// Ternary
{{ $json.status === 'active' ? 'Active' : 'Inactive' }}

// Math
{{ $json.price * 1.2 }}

// Array length
{{ $json.items.length }}
Enter fullscreen mode Exit fullscreen mode

Practical Examples

1. Dynamic email subject

Order confirmed for {{ $json.customer_name }}

2. Format date for Google Sheets

{{ $now.toFormat('MM/dd/yyyy') }}

3. Combine names

{{ $json.first_name + ' ' + $json.last_name }}

4. Safely handle a missing field

{{ $json.middle_name ?? 'N/A' }}

The ?? nullish coalescing operator returns N/A if the field is null or undefined.

5. Pull data from a different branch

In a Merge node after two parallel branches:

Customer: {{ $('Get Customer').item.json.name }}
Order total: {{ $('Get Order').item.json.total }}
Enter fullscreen mode Exit fullscreen mode

6. Build a URL dynamically

https://api.example.com/users/{{ $json.user_id }}/orders/{{ $json.order_id }}
Enter fullscreen mode Exit fullscreen mode

Paste directly into the URL field of an HTTP Request node.


Common Mistakes

Mistake Fix
json.name missing $ Use $json.name
Spaces inside braces {{ $json.name }} not { {$json.name} }
Wrong node name casing Names are case-sensitive
Array without index Use $json.items[0] not $json.items
Expression in static field Click = icon to switch to expression mode

Free Workflow JSON

Here is a minimal workflow demonstrating four expression patterns - dynamic URL, formatted date, ternary condition, and name concat:

{
  "name": "n8n Expressions Demo",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "values": {
          "string": [
            { "name": "user_id", "value": "42" },
            { "name": "status", "value": "active" },
            { "name": "first_name", "value": "Ada" },
            { "name": "last_name", "value": "Lovelace" }
          ]
        }
      },
      "name": "Set Sample Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [460, 300]
    },
    {
      "parameters": {
        "values": {
          "string": [
            { "name": "full_name", "value": "={{ $json.first_name + ' ' + $json.last_name }}" },
            { "name": "api_url", "value": "=https://api.example.com/users/{{ $json.user_id }}" },
            { "name": "label", "value": "={{ $json.status === 'active' ? 'Active' : 'Inactive' }}" },
            { "name": "today", "value": "={{ $now.toFormat('yyyy-MM-dd') }}" }
          ]
        }
      },
      "name": "Build Expressions",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [680, 300]
    }
  ],
  "connections": {
    "Start": { "main": [[{ "node": "Set Sample Data", "type": "main", "index": 0 }]] },
    "Set Sample Data": { "main": [[{ "node": "Build Expressions", "type": "main", "index": 0 }]] }
  }
}
Enter fullscreen mode Exit fullscreen mode

Import it into n8n, run it once, and inspect the output of Build Expressions to see all four patterns live.


Want More Ready-to-Run Workflows?

The patterns above power every automation I build. If you want a full library of production-ready workflows - error handling, webhooks, Stripe receipts, CRM sync, and more - I packaged them into the n8n Workflow Starter Pack ($29).

One purchase, instant download, no subscription.


Quick Reference

Variable What it gives you
$json.field Field from the current item
$('Node Name').item.json.field Field from a named previous node
$now Current time (Luxon DateTime)
$vars.NAME Workflow variable
$env.NAME Environment variable (self-hosted)
$input.all() All items entering the current node
$input.first().json First item only
$input.last().json Last item only

Expressions unlock the difference between a rigid script and a truly dynamic workflow. Once they click, you will use them in every single node.

Drop a question below if you get stuck on a specific expression - happy to help debug it.

Top comments (1)

Collapse
 
pirateprentice profile image
Pirate Prentice

What expression do you use most in your n8n workflows?

For me it is $now.toFormat("yyyy-MM-dd") - I need a clean date string constantly for Sheets, email subjects, and file names.

Also curious: has anyone hit a case where $json gave you unexpected data and you had to switch to $("Node Name").item.json to get what you needed? That cross-node reference trips people up the first time.