Lesson 5
JSON to CSV and Flattening
Export object arrays to CSV and flatten nested JSON into spreadsheet-friendly columns.
JSON can be nested:
[
{
"id": 1,
"profile": {
"region": "eu"
}
}
]
CSV is flat. To export this for a spreadsheet, nested fields need a column convention:
id,profile.region
1,eu
This is often called flattening.
Arrays need a policy
Arrays inside JSON objects do not map cleanly to a single CSV cell. Common choices are:
- Keep the array as a JSON string in one cell.
- Explode the array into multiple rows.
- Export a separate related CSV file.
The right choice depends on who consumes the CSV.
Missing fields
In an array of objects, not every object has the same keys. A CSV export should build a union of fields and leave missing cells empty.
Key takeaway
JSON → CSV requires flattening decisions. Choose column names that are understandable to the spreadsheet user and reversible enough for your workflow.
Use the JSON to CSV mode to preview flattened columns before sharing the file.