JSON to CSV: Keep Columns From Later Rows
When objects have different keys, a CSV exporter can use the first object to select columns. If a field appears only in a later object, check the header before you import the result elsewhere.
Worked example
Uneven input and observed output
[
{
"name": "Maya"
},
{
"name": "Alex",
"team": "Support"
}
]
name
Maya
AlexAligned JSON input
[
{
"name": "Maya",
"team": null
},
{
"name": "Alex",
"team": "Support"
}
]Actual corrected CSV
name,team Maya, Alex,Support
Steps
- List the columns you want in the CSV and decide how to represent missing values.
- Give every row the same keys, including the first. The example uses null for a missing team, which becomes an empty field in the export.
- Convert the aligned objects. Verify the header includes each expected column and check one row that originally introduced a new key.
What to check
- An empty CSV cell may mean missing or empty depending on the destination. Preserve a separate status column if that distinction matters.
- Decide how to represent nested fields before flattening; use columns such as address_city where the receiving schema expects them.