JSON to CSV Conversion: A Complete Developer Guide
CSV (Comma-Separated Values) is the universal format for spreadsheets, data analysis and legacy system integration. Analysts use Excel and Google Sheets. Data scientists use pandas. ETL pipelines expect CSV. Whenever you have JSON data from an API that needs to go into any of these systems, JSON to CSV conversion is the bridge.
This guide covers the structure requirements for conversion, how to handle nested JSON, code examples in JavaScript and Python, and how to use IRJSON's free online converter.
Structure Requirement: JSON Arrays of Objects
CSV conversion works best when your JSON is an array of objects where each object has the same keys. Each object becomes a row; each key becomes a column header.
// Ideal structure for CSV conversion — array of flat objects
[
{ "id": 1, "name": "Alice", "email": "alice@example.com", "role": "admin" },
{ "id": 2, "name": "Bob", "email": "bob@example.com", "role": "editor" },
{ "id": 3, "name": "Carol", "email": "carol@example.com", "role": "viewer" }
]
Result CSV:
id,name,email,role
1,Alice,alice@example.com,admin
2,Bob,bob@example.com,editor
3,Carol,carol@example.com,viewer
Handling Nested JSON
Nested objects cannot be represented directly in a flat CSV grid. You have two options:
Option 1: Flatten — convert nested objects to dot-notation keys:
// Nested JSON
{ "user": { "name": "Alice", "address": { "city": "Auckland" } } }
// After flattening
{ "user.name": "Alice", "user.address.city": "Auckland" }
Option 2: Ignore — exclude nested objects and only export flat values. Simpler but loses data.
Arrays within objects present a similar problem: they cannot be directly represented in a single CSV cell. Common approaches: join array values with a separator (|), create one row per array element, or JSON-stringify the array into the cell.
Convert JSON to CSV Online with IRJSON
JSON to CSV in JavaScript
Manual Implementation
function jsonToCsv(data) {
if (!Array.isArray(data) || data.length === 0) return ''
// Collect all unique headers across all objects
const headers = [...new Set(data.flatMap(row => Object.keys(row)))]
const csvRows = [
headers.join(','),
...data.map(row =>
headers.map(h => {
const val = row[h] ?? ''
const str = typeof val === 'object' ? JSON.stringify(val) : String(val)
// Wrap in quotes if it contains comma, quote or newline
return /[,"
]/.test(str) ? `"${str.replace(/"/g, '""')}"` : str
}).join(',')
)
]
return csvRows.join('
')
}
const csv = jsonToCsv([
{ id: 1, name: 'Alice', city: 'Auckland, NZ' },
{ id: 2, name: 'Bob', city: 'London' }
])
// id,name,city
// 1,Alice,"Auckland, NZ"
// 2,Bob,London
Using Papa Parse
import Papa from 'papaparse'
const csv = Papa.unparse([
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' }
])
// Download as file
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'data.csv'
a.click()
JSON to CSV in Python
Using the csv Module
import csv, json, io
data = [
{"id": 1, "name": "Alice", "email": "alice@example.com"},
{"id": 2, "name": "Bob", "email": "bob@example.com"}
]
output = io.StringIO()
writer = csv.DictWriter(output, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
print(output.getvalue())
Using pandas for Nested JSON
import pandas as pd
nested = [
{"id": 1, "user": {"name": "Alice", "city": "Auckland"}},
{"id": 2, "user": {"name": "Bob", "city": "London"}}
]
# json_normalize flattens nested objects
df = pd.json_normalize(nested)
# Columns: id, user.name, user.city
df.to_csv('output.csv', index=False, encoding='utf-8-sig') # utf-8-sig adds BOM for Excel
Tips for Better CSV Output
- UTF-8 BOM for Excel — add
at the start of your CSV to ensure Excel reads it as UTF-8 correctly on Windows - Consistent headers — if some objects have missing keys, use
nullor empty string as the value for that row - Quote all string values — safer than quoting only when necessary, avoids edge cases with values containing commas
- Dates as ISO 8601 — store dates as
2026-08-01strings; Excel will recognise the format
Frequently Asked Questions
Can I convert nested JSON to CSV?
Yes, but nested JSON must be flattened first. Nested objects become dot-notation columns (e.g. user.name). IRJSON's converter handles one level of nesting automatically. For deeper nesting, use pandas json_normalize in Python or a dedicated flattening library.
How do I handle special characters in CSV?
Values containing commas, double quotes or newlines must be wrapped in double quotes. Any double quotes within the value are escaped by doubling them (e.g. "She said ""hello"""). All good CSV libraries handle this automatically.
Why does my CSV look wrong in Excel?
Excel on Windows defaults to a Windows encoding rather than UTF-8, which corrupts non-ASCII characters. Add a UTF-8 BOM (\ufeff) at the start of the file. IRJSON's CSV export includes the BOM by default for Excel compatibility.
Convert JSON to CSV online free
IRJSON converts JSON arrays to CSV with one click. No sign-up, no file uploads, 100% private.
Open Converter →