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.

TipIntermediate2 min read

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).
```text
```javascript
// 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;
```text
> **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](../code-node-mastery/01-flatten-deeply-nested-api-responses.md) | [Use the HTTP Request Node as a Universal Connector](../integration-patterns/01-use-the-http-request-node-as-a-universal-connector.md)

Want this running in your stack?

I build production n8n and Cloudflare automation for teams — the same engineering behind HarperFlow. Fixed-price, escrow-protected, US-based.