Tips > Data, APIs & Webhooks

Understand `$json` vs `$input.first()` vs `$('NodeName').first()`

n8n provides multiple ways to reference data, and using the wrong one causes subtle bugs.

n8n offers several ways to reference data, and the wrong one causes subtle bugs. Use $json for the current item, $input.first() for the first item of the preceding node, $input.all() for every item in a Code node, and $('NodeName') to reach a specific upstream node. Choosing correctly prevents hard-to-debug missing-data errors.

What is the difference between $json, $input.first(), and $('NodeName').first()?

n8n provides multiple ways to reference data, and using the wrong one causes subtle bugs. Understanding when to use each reference pattern is critical for building reliable workflows.

Real-world example: A workflow fetches a configuration record from a database (node: "Get Config"), then processes a batch of customer items. During batch processing, you need the config value alongside each customer item.

$json
  → The current item's JSON data in the current node.
  → Use in expressions that process each item: {{ $json.email }}

$input.first().json
  → The first item from the immediately preceding node.
  → Use when only one item is expected: {{ $input.first().json.api_key }}

$input.all()
  → All items from the immediately preceding node (Code node only).
  → Use for aggregation or cross-item logic.

$('NodeName').first().json
  → The first item output by a specific named node, anywhere upstream.
  → Use to reach back to a node that is not directly preceding.

$('NodeName').all()
  → All items from a specific named node (Code node only).

How do you access data from multiple upstream nodes?

// Code node — accessing data from multiple upstream nodes

// Current batch of items from the previous node
const customers = $input.all();

// Config value from a node earlier in the workflow
const apiEndpoint = $('Get Config').first().json.api_endpoint;
const batchSize = $('Get Config').first().json.batch_size;

const results = [];
for (const customer of customers) {
  results.push({
    json: {
      name: customer.json.name,
      endpoint: apiEndpoint,
      processedAt: new Date().toISOString()
    }
  });
}

return results;

What is the common pitfall to avoid?

Warning: Common Pitfall. Using $json inside a Code node that runs once for all items only gives you the first item. Use $input.all() and iterate. Conversely, $('NodeName').all() in an expression (outside Code) does not work -- expressions evaluate per item.

Choosing the right reference pattern prevents the "missing data" bugs that are hardest to debug in n8n.

Related: Flatten Deeply Nested API Responses · Use the HTTP Request Node as a Universal Connector

Showcase builds

19 complete workflows from my own projects, each with its n8n workflow JSON to import. Showcase entries link the file at the end of the article.

See the showcase builds

Keep reading

190 entries grouped by topic, from first workflow to queue mode. Free, no signup.

Browse the encyclopedia

Need it built?

I design, build and run n8n systems for clients. Every engagement starts with a $1,500 diagnostic audit, credited toward the build.

Book an introductory call