Converting JSON to CSV is one of the most common data transformation tasks in development. Whether you're exporting data for a non-technical user, importing into Excel, or feeding a machine learning pipeline, here are three reliable methods — no matter your stack.
Why convert JSON to CSV?
- Business users need data in spreadsheets (Excel, Google Sheets)
- CSV is required by many data import tools and databases
- CSV is faster to process for large flat datasets
- Reporting tools often require CSV format
What JSON structure works for CSV?
CSV is a flat format — it doesn't support nested structures. The ideal JSON for CSV conversion is a flat array of objects:
[
{ "name": "Alice", "age": 28, "city": "Paris" },
{ "name": "Bob", "age": 34, "city": "London" },
{ "name": "Carol", "age": 25, "city": "Berlin" }
]
Each object becomes a row. Each key becomes a column header.
Method 1: Online converter (instant, no code)
If you just need a one-time conversion, an online tool is by far the fastest option.
Try it free — JSON to CSV Converter
Paste your JSON array and download a clean CSV file instantly. Choose your delimiter, no signup needed.
Method 2: Convert JSON to CSV in JavaScript
function jsonToCSV(jsonArray) {
if (!jsonArray.length) return '';
// Get all unique keys as headers
const headers = [...new Set(jsonArray.flatMap(row => Object.keys(row)))];
// Build CSV rows
const escape = (val) => {
const str = val === null || val === undefined ? ''
: typeof val === 'object' ? JSON.stringify(val)
: String(val);
return str.includes(',') || str.includes('"') || str.includes('\n')
? `"${str.replace(/"/g, '""')}"`
: str;
};
const rows = jsonArray.map(row =>
headers.map(h => escape(row[h])).join(',')
);
return [headers.join(','), ...rows].join('\n');
}
// Usage
const data = [
{ name: "Alice", age: 28, city: "Paris" },
{ name: "Bob", age: 34, city: "London" }
];
const csv = jsonToCSV(data);
console.log(csv);
// name,age,city
// Alice,28,Paris
// Bob,34,London
// Download as file in browser
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url; a.download = 'data.csv'; a.click();
Method 3: Convert JSON to CSV in Python
import json
import csv
import io
def json_to_csv(json_data):
if isinstance(json_data, str):
data = json.loads(json_data)
else:
data = json_data
if not data:
return ''
output = io.StringIO()
writer = csv.DictWriter(output, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
return output.getvalue()
# Usage
data = [
{"name": "Alice", "age": 28, "city": "Paris"},
{"name": "Bob", "age": 34, "city": "London"}
]
csv_output = json_to_csv(data)
print(csv_output)
# Save to file
with open('output.csv', 'w', newline='') as f:
f.write(csv_output)
Using pandas (recommended for large datasets)
import pandas as pd
import json
# From a JSON file
df = pd.read_json('data.json')
df.to_csv('output.csv', index=False)
# From a JSON string
json_str = '[{"name":"Alice","age":28},{"name":"Bob","age":34}]'
df = pd.read_json(json_str)
df.to_csv('output.csv', index=False)
# From an API response
import requests
response = requests.get('https://api.example.com/users')
df = pd.DataFrame(response.json())
df.to_csv('users.csv', index=False)
Handling nested JSON
Nested JSON doesn't map cleanly to CSV. You have two options:
- Flatten it: Convert
{"address": {"city": "Paris"}}to{"address.city": "Paris"} - Stringify it: Store the nested object as a JSON string in the CSV cell
# Python: flatten nested JSON with pandas
df = pd.json_normalize(data) # automatically flattens nested keys
df.to_csv('flattened.csv', index=False)