How to flatten nested JSON into a table (online, Excel, Python)
Turn nested JSON, such as an API response, into rows and columns. How to handle nested objects and lists, with examples in Excel Power Query, pandas and jq.
APIs, webhooks and NoSQL databases hand you data as JSON, and JSON loves nesting: a customer with an address object inside it, an order with a list of items inside that. Spreadsheets and SQL tables want the opposite: one flat row per record, one value per cell.
“Flattening” is the step in between. Most of the JSON I flatten comes from REST APIs and event logs feeding a report or a warehouse table. The mechanics are easy. The decisions about lists are where the time goes, so this guide spends most of its time there.
This guide explains the two kinds of nesting, the choice each one forces on you, and how to do it with or without code.
Nested objects: easy
An object inside an object has exactly one value per field, so it flattens without any decisions. Each nested field becomes its own column, named by its path:
{ "id": 1, "name": "Asha", "address": { "city": "Pune", "country": "IN" } }
| id | name | address.city | address.country |
|---|---|---|---|
| 1 | Asha | Pune | IN |
Nested lists: you have to choose
Lists (arrays) are where it gets interesting, because a list can hold any number of values, and a cell holds one:
{ "order": 7, "customer": "Asha", "items": [ { "sku": "A1", "qty": 2 }, { "sku": "B2", "qty": 1 } ] }
There are three sensible ways to flatten this.
One row per item. The parent fields are repeated on each row:
| order | customer | items.sku | items.qty |
|---|---|---|---|
| 7 | Asha | A1 | 2 |
| 7 | Asha | B2 | 1 |
This is the right choice when you want to total, filter or pivot the items, for example “how many of A1 did we sell?”. Be careful when summing parent-level numbers afterwards, though: an order total repeated on two rows gets counted twice.
One column per item. Everything stays on one row, and positions get their own columns: items.1.sku, items.2.sku and so on. It’s good for short lists of fairly fixed length, like a primary and secondary phone number. It gets unwieldy when one record has 40 items.
Join into one cell. items.sku becomes A1, B2. It’s the most readable when you just want to look at the data, but the hardest to calculate with.
The quick way: in your browser
The Flatten JSON tool does all three. Drop in a JSON file or paste the text, pick how lists should be handled, and download CSV or Excel. If the JSON is wrapped in an object, like { "data": [ ... ] }, as most APIs do, the tool finds the list of records inside it for you. Nothing is uploaded.
In Excel (Power Query)
Excel can flatten JSON without add-ins:
- Data → Get Data → From File → From JSON, and choose the file.
- Power Query opens showing
ListorRecord. If it’s a list, click To Table and accept the defaults. - Click the expand icon (two arrows) in the column header. Pick the fields you want and untick Use original column name as prefix if you prefer short names.
- For a column that still shows
List, click its expand icon and choose Expand to New Rows. That’s the “one row per item” option. - Repeat until no
RecordorListcells remain, then Close & Load.
Power Query remembers these steps. Next time, replace the file and click Refresh.
In Python (pandas)
pandas.json_normalize handles nested objects automatically:
import json
import pandas as pd
with open("orders.json") as f:
data = json.load(f)
# One row per order, nested objects become address.city etc.
orders = pd.json_normalize(data)
# One row per item, with order fields repeated
items = pd.json_normalize(data, record_path="items", meta=["order", "customer"])
items.to_csv("order_items.csv", index=False)
Use sep="_" if you’d rather have address_city than address.city, which some databases prefer. If the API wraps records in a key, pass the inner list: pd.json_normalize(data["results"]).
On the command line (jq)
For quick work in a terminal, jq can flatten and output CSV:
jq -r '.[] | .order as $o | .items[] | [$o, .sku, .qty] | @csv' orders.json
This prints one CSV line per item. It’s fast and needs no Python, but you have to list the fields yourself.
Common problems
“Unexpected token” or “invalid JSON”. Usually a trailing comma after the last item, single quotes instead of double quotes, or a file that is really JSON Lines: one object per line, with no surrounding [ ]. The online tool accepts JSON Lines too.
Columns appear in some rows but not others. That’s normal. JSON records don’t have to share fields. Missing fields become empty cells.
From experience: for anything that goes into reporting, I don’t build one wide table. I build one table for the parent records and a separate table per list, linked by the parent ID. That way totals stay correct, and each table stays small enough to check.
Row counts explode. Two lists in the same record, both set to one row per item, multiply: 10 items × 10 payments = 100 rows per order. Flatten one list at a time into separate tables, then join them on the parent ID.
Going back the other way. To turn a flat CSV into JSON, use the CSV to JSON converter.