JSON to CSV with nested objects — pick how it flattens
Nested JSON has no place in a spreadsheet until it is flattened. Choose a strategy, watch the rows change, and take the code if you would rather do it in a terminal.
Flattening nested JSON in Python
This is the part that trips people up: a nested object has no column of its own, so it has to be flattened into one before it can be a cell. The function below joins the parent and child names with a double underscore.
import csv
import json
def flatten(obj, prefix="", sep="__"):
"""Turn nested dicts into a single level of key/value pairs."""
flat = {}
for key, value in obj.items():
name = f"{prefix}{sep}{key}" if prefix else key
if isinstance(value, dict):
flat.update(flatten(value, name, sep))
elif isinstance(value, list):
# Arrays of primitives become one cell; arrays of objects are
# handled by the detail strategy instead.
flat[name] = " | ".join(str(v) for v in value)
else:
flat[name] = value
return flat
with open("data.json", encoding="utf-8") as f:
rows = json.load(f)
flat_rows = [flatten(row) for row in rows]
fieldnames = list(dict.fromkeys(k for row in flat_rows for k in row))
with open("data.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames, restval="")
writer.writeheader()
writer.writerows(flat_rows)
Nested objects become address__city; arrays become one joined cell
Note fieldnames is built from every row, not just the first one. If the second record has a key the first one does not, that column is still created instead of silently vanishing. restval="" fills the gap for rows that do not have it.
When the nested value is an array of objects - say a list of line items - flattening is not enough. You want the detail strategy, where each array element becomes its own row and the parent values are repeated. The converter above can do all four strategies, so you can compare them on your own data.
A worked example
One object with a nested object and an array of strings, flattened two different ways.
customer.json
[
{
"id": 7,
"name": "Ada Lovelace",
"address": { "city": "London", "postcode": "E1 6AN" },
"skills": ["math", "pioneer"]
}
]
customer.csv
id,name,address__city,address__postcode,skills
7,Ada Lovelace,London,E1 6AN,math | pioneer
Flatten gives one row and joins the nested keys. The converter above defaults to a tab delimiter so the file pastes straight into Excel - switch it to comma in the settings to match this example. Switch the converter to Matrix and the same input produces two rows - one per skill - because the array is expanded instead of joined. Same data, different question answered by the table.
Pick a strategy and see the result
Four strategies are built into the converter above. They answer four different questions about the same data.
-
Paste the nested JSON
Objects, arrays, or both. The converter detects the shape automatically.
-
Open the settings cog
The Nested data dropdown holds the four strategies. The delimiter dropdown changes the output separator.
-
Compare the strategies
Header/detail repeats parent values per array element. Flatten joins keys. Concatenate keeps nested objects whole in one cell. Matrix expands every combination.
-
Download
The preview shows the exact header and row count for the strategy you picked, so you can tell the strategies apart before exporting.
Why flatten the way we do
The details that decide whether the resulting sheet is actually usable.
The parent value survives
If a child field has the same name as a parent field, the child key is qualified (items__id) so neither value is lost.
Arrays are not mangled
Arrays of primitives join with a pipe. Arrays of objects become their own rows under the header/detail strategy.
Stable column names
Nested keys join with a double underscore, which keeps them readable and safe as a spreadsheet header.
Try all four, then decide
The same input under four strategies makes the trade-off obvious before you commit to one output.
The full JSON to CSV guide
The guide covers flattening rules in detail, plus delimiters, quoting and how to choose a strategy for your data.
Related tools: JSON to CSV in Python · JSON array to CSV · JSON to SQL · CSV to JSON converter
Frequently asked questions
How nested JSON gets turned into columns, and what the four strategies each do.
How do I flatten nested JSON?
address__city. Arrays of primitives become one cell joined by a delimiter. Arrays of objects need the detail strategy, where each element becomes its own row.Which separator should I use for nested keys?
What happens to arrays inside an object?
How do I keep both the parent and the child value?
id, the child key is qualified with the array it came from - items__id - so both values survive into the CSV.How many rows will nested JSON produce?
Flatten your nested JSON
Free, instant, and private — your JSON never leaves this tab.