Flat JSON, nested JSON, JSON arrays — every case covered with browser tools, Python, JavaScript, and command line.
JSON is the standard format for APIs and web services. CSV is the standard format for spreadsheets and data analysis. You need to convert JSON to CSV when: (1) you pull data from an API and want to analyse it in Excel or Google Sheets, (2) a client or colleague needs data in a spreadsheet-friendly format, or (3) you are importing data into a system that accepts CSV but not JSON.
Input JSON array:
[
{"name": "Rahul", "age": 28, "city": "Mumbai"},
{"name": "Priya", "age": 25, "city": "Delhi"}
]
Output CSV:
name,age,city
Rahul,28,Mumbai
Priya,25,Delhi
This case is trivial — headers come from the keys of the first object, values come from corresponding keys in each subsequent object.
Input JSON:
[{"name": "Rahul", "address": {"city": "Mumbai", "pin": "400001"}}]
Naive conversion fails — "address" is an object, not a value.
Flattened correctly with dot-notation:
name, address.city, address.pin
Rahul, Mumbai, 400001
import json, csv, pandas as pd
# Simple method with pandas
df = pd.json_normalize(json.load(open('data.json')))
df.to_csv('output.csv', index=False)
# Without pandas (built-in only)
data = json.load(open('data.json'))
with open('output.csv', 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
const data = [
{ name: "Rahul", city: "Mumbai" },
{ name: "Priya", city: "Delhi" }
];
const headers = Object.keys(data[0]);
const csv = [
headers.join(','),
...data.map(row => headers.map(h => `"${row[h]}"`).join(','))
].join('\n');
// In Node.js:
require('fs').writeFileSync('output.csv', csv);