How to Convert JSON to CSV: Tutorials & Interactive Tool
The definitive 2026 guide for converting JSON and JSONL to CSV spreadsheets. Includes production code snippets for Python (pandas), Node.js, Bash (jq), C#, and an instant browser converter.
Quick Browser Sandbox: Convert JSON to CSV Instantly
Sample JSON Input
CSV Output
1. Convert JSON to CSV in Python (Pandas vs. Standard Library)
Python is the industry-standard choice for data transformation pipelines. Depending on whether you have external dependencies or need zero-install scripts, choose between pandas and Python's built-in csv module:
pandas_converter.py (Best for Nested JSON)
import pandas as pd
# Method 1: Load flat JSON array directly
df = pd.read_json('data.json')
# Method 2: If data contains nested objects, normalize them:
# import json
# with open('data.json') as f:
# raw_data = json.load(f)
# df = pd.json_normalize(raw_data)
# Export DataFrame to CSV without row index numbers
df.to_csv('output.csv', index=False, encoding='utf-8')
print("Successfully generated output.csv")native_csv_writer.py (Zero Dependencies)
import json
import csv
with open('data.json', 'r', encoding='utf-8') as f:
records = json.load(f)
# Collect all unique keys across all records to avoid missing columns
headers = list({key for item in records for key in item.keys()})
with open('output.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
writer.writerows(records)
print(f"Exported {len(records)} rows to output.csv")2. Convert JSON to CSV in JavaScript & Node.js
In Node.js, you can convert arrays of objects without pulling in bulky npm packages. This native snippet safely escapes double quotes and joins keys:
convert.js (Node.js Native)
const fs = require('fs');
const raw = fs.readFileSync('data.json', 'utf8');
const data = JSON.parse(raw);
// Extract unified set of headers
const headers = Array.from(new Set(data.flatMap(item => Object.keys(item))));
// Format rows according to RFC 4180
const csvRows = [
headers.join(','),
...data.map(row =>
headers
.map(field => {
const val = row[field] !== undefined ? String(row[field]) : '';
// Escape quotes and wrap in double quotes if commas/newlines exist
return `"${val.replace(/"/g, '""')}"`;
})
.join(',')
)
];
fs.writeFileSync('output.csv', csvRows.join('\n'), 'utf8');
console.log('Saved output.csv');3. Convert JSON to CSV in Terminal with jq (Bash Pipeline)
For DevOps engineers and Linux sysadmins, jq provides the fastest command-line utility for streaming gigabytes of server logs into CSV without loading entire files into Python or RAM:
terminal.sh (Bash / jq)
# Extract all object keys dynamically and output RFC-compliant CSV
jq -r '(.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv' data.json > output.csv
# Or for JSONL (newline-delimited JSON) streams:
jq -r '[.id, .name, .email, .role] | @csv' logs.jsonl > logs.csv4. Best Practices for Flattening Deeply Nested JSON Payloads
- Dot-Notation Hierarchy: Convert nested child records (such as
user.address.postalCode) into clean dot-notated column names to prevent duplicate or scrambled rows. - JSONL vs. Array Parsing: When handling multi-gigabyte log dumps from AWS CloudWatch or Datadog, serialize line-by-line as JSONL rather than enclosing them in a single massive array.
- RFC 4180 Escaping: Always ensure strings containing commas, line breaks (
\n), or quotes are properly enclosed in double quotes with inner quotes doubled (""). - Zero Server Retention: If converting company data, avoid untrusted third-party web scrapers. Use our client-side converter or locally executed terminal scripts.