n8n has a ready-made node for most office jobs. Then there's the leftover: a total nobody planned for, a format that's slightly off, a rule with too many exceptions. That's what the Code node is for.

The example here is a made-up staffing agency. Every Friday someone builds a pivot table to total each client's hours before invoicing. One Code node does it: total the hours per client and work out the invoice amount at $42 an hour. Then the same job in Python, so you can see what changes.

Manual trigger → Sample timesheet → Code (JavaScript) ↘ Code (Python) 

No credentials needed.

The input

Seven timesheet rows, three clients, each on more than one row:

return [ { worker: 'Ana Ruiz', client: 'Kessler Logistics', hours: 7.5 }, { worker: 'Ben Carter', client: 'Northgate Dental', hours: 8 }, { worker: 'Ana Ruiz', client: 'Kessler Logistics', hours: 8 }, { worker: 'Chloe Park', client: 'Alder & Finch Law', hours: 6 }, { worker: 'Ben Carter', client: 'Northgate Dental', hours: 4.5 }, { worker: 'Dev Malhotra', client: 'Kessler Logistics', hours: 8 }, { worker: 'Chloe Park', client: 'Alder & Finch Law', hours: 7 }, ].map((json) => ({ json })); 

That's itself a Code node (the "Sample timesheet" step). Look at the last line: every object is wrapped as { json: ... }. That's the rule.

The one rule: items in, items out

Every n8n node receives a list of items and passes a list of items on. The Code node is no different, except you write the middle.

  • Read the input with $input.all(). Each item's data is under item.json.
  • Return a list of items, each with its data under a key called json.

Nearly every Code node error is a broken version of that rule: returning a bare value, returning one object instead of a list, or forgetting the json key. If you see an error saying the node must return an array of objects, that's what happened.

Mode: all items or each item

Above the code editor there are two settings. Mode has two choices:

Mode Runs Good for
Run Once for All Items One time, sees the whole list Totals, rankings, summaries, de-duplication
Run Once for Each Item Once per item A small change to every row, like splitting a full name

Adding things up across rows needs the first. In "each item" mode your code only ever sees one row, so it can't total anything.

The JavaScript

Add a Code node after the sample, keep Mode: Run Once for All Items and Language: JavaScript, select the starter code and paste over it:

const totals = {}; for (const item of $input.all()) { const row = item.json; totals[row.client] = (totals[row.client] || 0) + row.hours; } return Object.entries(totals).map(([client, hours]) => ({ json: { client, hours, invoice: hours * 42 }, })); 

Read it once:

  1. Loop over every incoming item.
  2. Add its hours to that client's running total.
  3. Turn the totals object into a list, one item per client, each wrapped in { json: ... }, with hours * 42 as the invoice.

Execute step. Seven items in, three out:

client hours invoice
Kessler Logistics 23.5 987
Northgate Dental 12.5 525
Alder & Finch Law 13 546

Check one by hand before you trust it: Kessler is 7.5 + 8 + 8 = 23.5, and 23.5 × 42 = 987. Good.

The same job in Python

Open Language and choose Python. The starter code changes, and so does the hint under the editor: input comes from _items, and imports are off unless your instance allows them.

totals = {} for item in _items: row = item["json"] totals[row["client"]] = totals.get(row["client"], 0) + row["hours"] return [ {"json": {"client": client, "hours": hours, "invoice": hours * 42}} for client, hours in totals.items() ] 

Same three items out.

What's different in Python:

JavaScript Python
All items $input.all() _items
One item (each-item mode) $input.item / $json _item
$ helpers Yes No
Imports — Disabled unless the instance allows them
Internet access — None (sandboxed)
Return shape List of { json: {...} } List of {"json": {...}}

Plain Python with loops, dictionaries and list comprehensions covers most reshaping jobs. If you need a library or data from the web, fetch it with the HTTP Request node and process the result in code, or use JavaScript.

Which to pick? JavaScript is n8n's home language: it has the $ helpers, date tools and fewer limits. Use Python when you or your team read it more easily and the job needs no libraries.

When not to use code

Reach for a ready-made node first. They're easier for the next person to read:

  • Filter and Edit Fields for keep-this, rename-that.
  • If and Switch for routing.
  • Summarize for simple totals.

Use the Code node when the job would need two or three of those chained awkwardly, or a rule none of them can express. And when you do, add error alerts, because code is where surprises hide.

You don't have to write it from scratch

You need to be able to read a short loop, not write one cold. A workable approach: describe the input (paste one item's JSON) and the output you want to an AI assistant, remind it of the { json: ... } return shape, paste what it gives you, run it on test data, and check one result by hand like the Kessler check above.

Checklist

  • [ ] Mode is Run Once for All Items for totals
  • [ ] Input read with $input.all() (JS) or _items (Python)
  • [ ] Every returned item is wrapped in json
  • [ ] Return value is a list, even for one result
  • [ ] One output checked by hand
  • [ ] No imports in Python unless your instance allows them

Practice it

This is a free step-by-step lesson where you build both versions in a practice copy of n8n (15 steps, about 6 minutes, no account), with the workflow JSON to import: n8n: Run JavaScript or Python in the Code Node.