Automation · n8n

n8n Data Transformation Patterns

Most n8n frustration is really data-shape frustration. Once the item model clicks, transformation is a choice between expressions, the Set node, and the Code node.

John Kihiu12 min read

Data transformation is where n8n workflows are actually built or broken, and where newcomers struggle — not because it is hard, but because n8n's item-based model is unfamiliar. Understand how items flow, and choosing the right transformation tool becomes obvious.

The item model

Data moves between n8n nodes as an array of items, each a JSON object. A node runs once per incoming item by default, and outputs items to the next node. Almost every "why is this looping" or "why do I only get one result" confusion is a mismatch between how many items you think you have and how many you actually do. Watch the item count on each node — it is the single most useful debugging habit in n8n.

Set, expression, or Code

NeedUse
Rename, add, or drop a few fieldsSet node
Compute one value inlineAn expression on a field
Reshape arrays, merge, complex logicCode node (JavaScript)
Custom per-item mapping at scaleCode node, run-once-per-item mode

Reach for the Set node for straightforward field mapping — it is declarative and readable. Use expressions ({{ }}) for quick inline computation drawing on data from earlier nodes. Drop to the Code node only when the logic genuinely needs it: reshaping nested arrays, aggregating across items, anything a Set node would turn into a tangle.

JavaScript · reshape items in a Code node
// Flatten Acumatica line items into one item per line
const out = [];
for (const item of $input.all()) {
  for (const line of item.json.Details) {
    out.push({ json: {
      order: item.json.OrderNbr.value,
      sku:   line.InventoryID.value,
      qty:   line.Quantity.value,
    }});
  }
}
return out;

Arrays and nested data

ERP and API payloads are deeply nested, and this is where the Code node earns its place. Use it to flatten nested arrays into flat items, or the Item Lists node to split an array field into individual items. The rule of thumb: get the data into the shape where one item equals one thing you want to act on, as early as possible, and the rest of the workflow becomes simple.

Pin data while you build

n8n lets you pin the output of a node so you can iterate on downstream transformations without re-calling the source API every time. Pin a real sample response and build your mapping against it — faster, deterministic, and it saves you from burning API quota while you experiment.

Transformation in n8n is a small toolkit used well: know the item model, prefer the Set node and expressions for simple shaping, drop to the Code node for real reshaping, and flatten to one-item-per-thing early. Pin sample data while you build, and the transformations stop fighting you.

John Kihiu
Acumatica ERP Developer · Laravel Engineer

Independent software engineer in Nairobi specialising in Acumatica customisations, Laravel backends, and tax fiscalisation integrations across East and Southern Africa.