Ir para o conteúdo

Lição 6 · Data and Expressions

Items and JSON: How Data Flows in n8n

Learn the data structure n8n passes between nodes: items with a json key, lists and nested objects, binary data, and how to reshape one item into many and back.

Nearly every confusing moment in n8n comes down to the shape of the data. If you can read that shape, you can write the right expression, choose the right node and debug quickly. n8n passes an array of items between nodes, and each item keeps its main data under a key called json.

The shape of an item

JSON
[
  {
    "json": {
      "id": 1,
      "name": "Amara",
      "tags": ["vip", "newsletter"],
      "address": { "city": "Accra", "country": "GH" }
    }
  },
  {
    "json": { "id": 2, "name": "Kenji", "tags": [], "address": { "city": "Osaka" } }
  }
]

This is two items. The editor usually shows them as a table, with a toggle to view raw JSON. The table view is easy to read, but switch to JSON view when something looks odd: it shows whether a value is text, a number, a list or an object.

JSON in one minute

JSON type Example Watch out for
string "42" Looks like a number but is text
number 42 No quotes
boolean true "false" as text is not false
null null "Known to be empty"
array ["a","b"] An ordered list
object {"k":1} Named values

A missing key is different from a key set to null. APIs use both, so your workflow should cope with either.

Items versus lists

An important distinction:

  • Three items means three separate records. Most nodes will act three times.
  • One item containing a list of three is a single record. A node will act once.

Many APIs return the second form, for example { "orders": [ ... ] }. To act on each order, you turn the list into items with the Split Out node. To do the reverse, combining items into one list, use Aggregate. Both appear in transforming data.

Python Stand-in: split a list into items and combine it again

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.

Reading nested values

Use dots and brackets to go deeper:

TEXT
{{ $json.address.city }}
{{ $json.tags[0] }}
{{ $json['order id'] }}

Brackets with quotes are needed when a key contains spaces or symbols. If the parent object may be missing, optional chaining keeps an expression from failing:

TEXT
{{ $json.address?.country ?? 'unknown' }}

Binary data

Files such as images and PDFs travel as binary data, stored apart from json. An item can carry both. Nodes that read or write files let you choose which binary property to use. Remember that large files use memory and storage; process them only when needed.

What happens to fields

Each node defines its own output. Some keep every input field and add new ones; others return only what they created. After a database "insert" node, for example, you may get just the database's reply, with your original fields gone. Check the output panel, and use a reference to an earlier node if you need old values.

JS Stand-in: losing and keeping fields

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

No output.

Item linking

n8n remembers which input item each output item came from. This is called item linking. It lets a later node ask an earlier node for "the matching item", even after filtering. Most nodes keep the link automatically. It can break when code creates new items without saying where they came from. You will see the fix in the Code node lesson.

Common mistakes

  • Treating a list inside one item as several items. Check the item count in the output panel.
  • Comparing text with numbers: "10" and 10 are different values.
  • Assuming a field exists in every item. Real data has gaps.

A habit for new data sources

Before you write any expression for a new source, run it once and write down its shape in plain words, for example: "one item, which holds data, a list of orders; each order has id, total as text and an optional coupon." That sentence tells you that you need Split Out on data, a number conversion for total and a fallback for coupon. It takes a minute and prevents many mapping errors. When a service changes its response format, compare the new shape against your sentence to find what broke.

Practice

Sketch the JSON for a school timetable with two classes, each having a teacher, a room and a list of lessons. Write it as an array of items. Then answer these questions: how many items are there, what expression reads the first lesson of an item, and which node turns lessons into separate items? Write the expression that safely reads the room even when it is missing. Finally, convert your design into the opposite shape, one item with a list of classes, and note which node would convert it back. Doing this on paper makes data shapes feel natural.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Where does an item keep its main data?

    Each item holds its record under the json key.
  2. 2. An API returns one item with a list of 20 orders. How can you act on each order?

    Split Out turns each list element into its own item.
  3. 3. Which expression safely reads a possibly missing country?

    Optional chaining avoids an error and the fallback supplies a value.

Summary

  • n8n passes an array of items, each with data under json.
  • A list inside one item is not the same as many items.
  • Split Out and Aggregate convert between the two.
  • Nodes shape their own output, and item linking connects items across nodes.

Next: Expressions in n8n

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