
An order export from a shop is not a table. Each order holds a list of line items, maybe a list of refunds, a few discount codes and a comma-separated string of tags. A CSV has one row per thing. So before the file goes anywhere you have to choose what a row is, and both obvious choices break something.
We worked it on a file with 500 orders. One row per order keeps every total right but hides the items in a JSON cell. One row per item gives you the items, and every order-level amount repeats on each of them: summing total_price over the item rows gives 126,859.91 where the orders add up to 46,182.20. Then a second list, refunds, makes it worse. Here are the numbers, the SQL that rebuilds the order totals from the items, and a check that tells you when you lost a row.
The file we used
It is not a real shop. We generated it with a script, in the shape of Shopify's REST Admin order (money as strings, line items with their own tax lines, a shipping line, refunds with refund line items, tags as one comma-separated string). Shopify has called REST a legacy API since October 2024, but the shape is still what most exports and tutorials show, and the GraphQL bulk export you would use now nests the same things. Invented customers, invented products, real structure.
Every order adds up before we touch it: line items minus discounts, plus shipping, plus tax equals total_price, to the cent, on all 500. That matters later, because it gives us a check to run on every shape.
Step zero: the file is one object, not a list
Shopify wraps the list: {"orders": [ ... ]}. Our dataConverter reads a top-level object as a single row. We dropped the whole file in and got 1 row with 1 column called orders, holding all 500 orders as one JSON string. That is not a bug, it is a file with one object in it, but it looks like the tool failed.
Take the list out first. In jsonTools open the JSONPath tab and run $.orders[*]. The result is a bare array of 500 orders. Paste that back (or save it) and convert.
Way one: one row per order
With the bare array in, dataConverter gave 500 rows and 17 columns. Nested objects become dotted columns (customer.id, customer.email). Arrays are not opened: discount_codes, line_items, shipping_lines and refunds each stay in one cell as JSON text.

- Row count equals the order count: 500.
- SUM(total_price) over the file is 46,182.20, the true figure.
- You can still count and group by anything on the order: status, tag, customer, day.
- You cannot ask "which SKU sold most" in a spreadsheet: the SKUs are inside a text cell, up to 881 characters wide.
- Excel and Sheets cut a cell at 32,767 characters, so a very long order is the first thing to break. Ours topped out far below that.
This is the right shape for anything about orders. It is the wrong shape for anything about products.
Way two: one row per item
Two routes. The quick one is the same JSONPath tool: $.orders[*].line_items[*] returns a flat array of 1,074 items, and dataConverter turns that into 1,074 rows.
![jsonTools running the JSONPath $.orders[*].line_items[*] on the order file: the result is a flat list of line item objects with id, title, sku, quantity, price and tax lines](https://pilotbase.pro/media/pilotbase/nested-json-orders-jsonpath-items.jpg)
Look at what came out: id, title, SKU, quantity, price, discount, tax. No order number. The items in our file do not carry one, and the path walked straight past the parent. Check yours; if your items have no order id either, you now have 1,074 rows you cannot join back to anything. Good enough for "how many of each SKU", useless for "which orders".
The route that keeps the parent is SQL. Load the 500-row CSV into a table with the array columns as JSON, then open the array and carry the order id along. We ran the SQLite and PostgreSQL 18 versions below on this file; the MySQL one is written from the manual and we did not run it.
PostgreSQL
orders_raw is the 17-column CSV loaded with line_items, refunds and the other arrays as jsonb. COPY ... WITH (FORMAT csv, HEADER true) takes the JSON text as it is. Result: 1,074 rows.
SQLite
Also 1,074 rows (SQLite 3.45). Notice the REAL: money as floating point, which is why the check further down needs a tolerance there. Use integer cents if you can; our money in SQL article shows what the rounding costs.
MySQL 8 (not run for this article)
What doubles, and by how much
Here is the trap. On the item rows, every order-level column is repeated once per item. Sum one of them and each order counts as many times as it has items. We summed the same columns both ways:
The factor is not the average item count (2.15 items per order). Big orders have more items, so they are counted more times, and they are also the big amounts. Shipping is the smallest factor because orders of 75 or more ship free in this file, and the big orders are the free ones. So you cannot fix it with one division.
Item-level columns are fine to sum on item rows. Tax rebuilt from the item tax lines is 3,291.05, exactly the order-level total. That is the rule in one line: a number belongs to the row grain it was measured at. Sum it only there.
Three ways to keep the order amount out of your sums
- Two tables. orders (one row each, the totals) and order_items (one row each, the item amounts, plus order_id). Join only when you need both, and never SUM an order column after a join. This is what we would do.
- First row only. If you must keep one flat file, number the rows per order and count the order amount once: SUM(total_price) FILTER (WHERE rn = 1) with row_number() OVER (PARTITION BY order_id ORDER BY line_item_id). We got 46,182.20 back. Everyone downstream has to know about rn, which is the weakness.
- Allocate it. If you want shipping or a discount per item so each item row carries its share, split the amount by weight and keep the pennies exact. amountSplit does that with largest remainders so the parts add back to the total.
Shopify's own order CSV export is one row per line item too, with the extra items on separate lines, per its help page. Sources disagree whether the Total repeats on those rows or is left blank. Take one order with two items from your own export and look before you add a column up.
The second list: refunds multiply it again
Orders can hold more than one list. We have refunds, and each refund has its own refund_line_items. Open items and refunds in the same query and you get every item paired with every refund of that order.
Only 22 orders had two refunds, so the row count went from 1,074 to just 1,118. The sum went from bad to worse, and the refund amount over those rows is 6,836.02 against a true 3,053.26. The row count barely moved, which is why nobody notices. Open each list into its own table, never two lists in one query.
Refund lines, as their own table
109 rows, 79 distinct refunds, subtotal 3,053.26, the same figure as the refund transactions inside the JSON. The line_item_id points at order_items.line_item_id, so a refunded SKU is one join away without touching the order total.
Rebuild the order totals from the items
This is the check that proves the item table is complete. Add the items up and compare them with what the order says.
It returned 0 on all 500 orders. Then we broke it on purpose: delete the one line item with the lowest id and run the gross check again, inside a transaction we rolled back. It returned 1 order that did not add up. That is what a dropped row looks like, and it is the reason to keep this query next to the load, not just run it once.
On SQLite the same query needs abs(a - b) > 0.005 instead of <>, because the sums are floating point. It also returned 0 for all 500.
Tags are a third kind of nesting
tags is not an array in this export. It is one string, "vip, gift", 396 orders have one, and 7 distinct tags appear. Opening it is the same move with a string splitter instead of a JSON one, and the same rule applies: one row per order and tag, in a join table, never summed through an order column. We wrote that one up with the SQL for three engines in a comma-separated column into rows.
Which shape for which question
What we did not test
We did not use a real shop's export, so the amounts and ratios (2.75x and the rest) are for this file; yours depends on how many items your orders hold, and the method is the same. We did not run Shopify's own CSV export or the GraphQL bulk export (which nests children as separate lines linked by a parent id, a fourth shape). We ran no timings. IDs in this file are 13 digits and survive a JavaScript parse; 19-digit IDs do not, as we found in 200,000 IDs read from JSON. The MySQL statement is untested here. PostgreSQL 18 ran on a scratch database that we dropped afterwards.
What to do with this
Pick the grain first, then the file. If the question is about orders, one row each. If it is about products, a second table of items with the order id on every row. And before you trust either, run the totals check: it is five lines and it is the only thing that tells you a row went missing.
Got a nested JSON export? dataConverter turns it into CSV or Excel in your browser tab and jsonTools pulls out the list you want with a JSONPath. Nothing is uploaded.
Open dataConverterOnce the orders and items are tables, Pilotbase is a database client for Windows, macOS and Linux, or Docker on a server: load them, run the totals check and look at the rows.
Get Pilotbase
