← LeadFlowFree CSV repairLive demoPricingAsk a question
Measured, not guessedNo upload

CSV to nested JSON in n8n: one item per parent, rows as children

The CSV node hands you one item per row. Almost every API you send that data to wants something else: one object per order, per company, per contact - with that parent's rows inside it. This page is the conversion, done in your browser first so you can see the shape, and then as a Code node you can paste into n8n.

1. Paste the CSV - nothing is uploaded, this runs in the tab
The output appears here.

Grouping is by the exact key value, trimmed. A column whose value is the same on every row of a group stays on the parent; the columns that actually differ become the child objects. If nothing differs in a group, all non-key columns go to its single child, so no data is lost.

When the shape is right but the file is not

Wrong shape is easy. Wrong data is not. If the export also has ragged rows, a semicolon delimiter, names that arrived as ? or a group key that is spelled three ways, I fix the file and hand back the nested JSON plus a row report naming everything changed.

$49 · one export up to 5,000 rows
$79 · up to 25,000 rows
$99 · up to 100,000 rows
Back within 24 hours, 14-day refund if the result does not match this description.

Pay $49 - up to 5,000 rows $79 - 25,000 rows $99 - 100,000 rows

Not ready to pay? Mail the export and the first look is free - a human reads it and tells you what is wrong before you spend anything: perun.intelligence@gmail.com. Paying opens PayPal and charges dragana.karans@gmail.com; files and questions are handled from perun.intelligence@gmail.com.

Why the obvious wiring does not nest

The Read/Write Files from Disk node and the Extract from File (CSV) node both emit one item per data row. That is the correct behaviour and it is the source of the problem: after the CSV node, an order with three lines is three unrelated items, and the parent's own fields are repeated three times.

The built-in Aggregator gives you one array per column (item: [Widget, Gadget]). That is a different shape: it does not give each parent its own child array, so an API that wants {order: A1, items:[...]} still cannot read it. Folding per-parent children under a per-parent key is the part that has to be written, and it is small.

The Code node (tested on 5 fixtures)

Put this in a Code node set to Run Once for All Items, after the CSV node, and set KEY and CHILD to your columns. It was run against fixtures covering quoted commas, CRLF, a semicolon file, a BOM, blank lines, short rows and single-row groups; the shapes below are its actual output.

The same parser also lives in a small repository with 12 checks you can run yourself (node test.js, no dependencies) and a CLI that prints the nested JSON for a whole file: fejzadenis/n8n-csv-to-json.

// Code node - Run Once for All Items
const KEY = 'order_id';   // the column that identifies one parent
const CHILD = 'items';    // name of the child array

const items = $input.all();
// one item per row: { json: { order_id: 'A1', customer: 'Doe, Jane', ... } }
const rows = items.map(i => i.json);
if (!rows.length) return [];

const header = Object.keys(rows[0]);
const keyOf = r => String(r[KEY] ?? '').trim();

const groups = [];
const byKey = new Map();
for (const r of rows) {
  const k = keyOf(r);
  if (!byKey.has(k)) { const g = { key: k, rows: [] }; byKey.set(k, g); groups.push(g); }
  byKey.get(k).rows.push(r);
}

return groups.map(g => {
  const parent = { [KEY]: g.key };
  const varying = [];
  for (const col of header) {
    if (col === KEY) continue;
    const vals = g.rows.map(r => r[col]);
    if (vals.every(v => v === vals[0])) parent[col] = vals[0];
    else varying.push(col);
  }
  const childCols = varying.length ? varying : header.filter(c => c !== KEY);
  if (!varying.length) for (const c of childCols) delete parent[c];
  parent[CHILD] = g.rows.map(r => {
    const o = {};
    for (const c of childCols) o[c] = r[c];
    return o;
  });
  return { json: parent };
});

What it returns

Input, three rows, two orders:

order_id,customer,item,qty
A1,"Doe, Jane",Widget,2
A1,"Doe, Jane",Gadget,1
B2,"Smith, John","Bolt, 3/8",4

Output, two items:

[
  { "order_id": "A1", "customer": "Doe, Jane",
    "items": [ { "item": "Widget", "qty": "2" }, { "item": "Gadget", "qty": "1" } ] },
  { "order_id": "B2",
    "items": [ { "customer": "Smith, John", "item": "Bolt, 3/8", "qty": "4" } ] }
]

Note the second object: the whole order is one row, so nothing varies and the single child keeps every column. That is deliberate - a "fix" that drops the child objects leaves you with parents that have no data.

The four traps that survive a correct script

TrapWhat actually happens
qty arrives as "2"Every CSV value is a string. If the API wants a number, cast it inside the child loop - and decide what an empty cell means before you cast, because Number('') is 0, not null.
Semicolon exportThe CSV node's delimiter is a setting, not a guess. A semicolon file read with a comma delimiter usually does not error: it silently gives one column whose name is the whole header line.
The same key spelled three waysA1, a1 and A-1 are three parents. Trim and normalise deliberately; the grouping above trims but does not case-fold, so a1 and A1 stay separate on purpose.
Numbers that start with 0A ZIP or an account number read as a number loses its leading zero. Keep them as strings until the last possible node.

Do it on a real export

The converter at the top of this page is the same logic with a parser in front of it, and it never sends the file anywhere - open the network tab and check. If your file is the part that is broken rather than the shape, the CSV repair studio fixes encoding and ragged rows first, and this page covers the Invalid Record Length error that a single malformed row produces.

Published by LeadFlow. The free tools here are complete products, not demos: the repair studio, the converter and the demo run entirely in your browser. The paid work is the part a script cannot do - reading your actual export and deciding what each broken row should have said. Questions: perun.intelligence@gmail.com.