I remember the first time I needed to convert JSON to CSV. I was working on a data migration project, and the client had given me a massive JSON file with user data. They needed it in Excel. "No problem," I thought. "I'll just... wait, how do you convert JSON to CSV again?"
I spent the next hour googling, trying different Python scripts, and eventually cobbling together something that sort of worked. It was messy, and I knew there had to be a better way.
Since then, I've converted JSON to CSV (and back) more times than I can count. Sometimes it's for data analysis in Excel. Sometimes it's for importing into a database. Sometimes it's just because someone on the team doesn't speak JSON and needs a spreadsheet.
Here's everything I've learned about converting between these two formats, the gotchas to watch out for, and the tools that make it painless.
Why Convert Between JSON and CSV?
Before we dive into the how, let's talk about the why. These formats serve different purposes:
JSON is great for:
- APIs and web applications
- Nested, hierarchical data
- When you need to preserve data types (numbers, booleans, null)
- Complex data structures with arrays and objects
CSV is great for:
- Spreadsheet applications (Excel, Google Sheets)
- Simple, flat data tables
- Data analysis and reporting
- When non-technical users need to work with the data
The problem is, they're not always compatible. JSON can have nested objects and arrays. CSV is flat—it's rows and columns, period. Converting between them requires some decisions about how to handle that complexity.
Converting JSON to CSV: The Basics
Let's start with the simpler direction: JSON to CSV. Here's a straightforward example:
Input JSON:
[
{
"name": "John Doe",
"age": 30,
"email": "john@example.com",
"city": "New York"
},
{
"name": "Jane Smith",
"age": 25,
"email": "jane@example.com",
"city": "San Francisco"
}
]
Output CSV:
name,age,email,city
John Doe,30,john@example.com,New York
Jane Smith,25,jane@example.com,San Francisco
Simple, right? Each JSON object becomes a row. Each key becomes a column header. But what happens when your JSON gets more complex?
The Nested Data Problem
Here's where it gets tricky. What if your JSON looks like this?
[
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "New York",
"zip": "10001"
},
"hobbies": ["reading", "coding", "traveling"]
}
]
You can't just flatten this into CSV directly. You have a few options:
- Flatten the nested object:
address.street,address.city,address.zip - Stringify the nested data: Convert the entire address object to a JSON string
- Create multiple rows: One row per hobby (denormalize)
- Use a delimiter: Join the hobbies array with semicolons or pipes
There's no "right" answer—it depends on what you need. For Excel users, flattening usually works best. For databases, you might want to denormalize.
Method 1: Online Tools (The Quick Way)
For one-off conversions, online tools are unbeatable. No code, no setup, just paste and convert.
I use our CSV to JSON Converter for this all the time. It handles both directions, supports custom delimiters, and runs entirely in your browser (so your data never leaves your computer).
Here's how it works:
- Paste your JSON (or upload a file)
- Choose your options (how to handle nested data, delimiter preferences)
- Click convert
- Download or copy the CSV
Best for: Quick conversions, one-time tasks, when you don't want to write code.
Method 2: Python (The Programmer's Way)
If you're doing this programmatically, Python is the way to go. The pandas library makes it trivial:
import pandas as pd
import json
# Read JSON file
with open('data.json', 'r') as f:
data = json.load(f)
# Convert to DataFrame
df = pd.DataFrame(data)
# Handle nested objects by flattening
# If you have nested data, you might need:
# df = pd.json_normalize(data)
# Save to CSV
df.to_csv('output.csv', index=False)
For nested data, pandas.json_normalize() is your friend:
import pandas as pd
import json
with open('nested_data.json', 'r') as f:
data = json.load(f)
# This automatically flattens nested objects
df = pd.json_normalize(data)
# For arrays, you might need to explode them
# df = df.explode('hobbies')
df.to_csv('output.csv', index=False)
Best for: Batch processing, automation, when you need to transform the data during conversion.
Method 3: JavaScript/Node.js
If you're working in a JavaScript environment, here's a simple converter:
function jsonToCsv(jsonData) {
if (!Array.isArray(jsonData) || jsonData.length === 0) {
return '';
}
// Get headers from first object
const headers = Object.keys(jsonData[0]);
// Create CSV header row
const csvRows = [headers.join(',')];
// Convert each object to a CSV row
for (const row of jsonData) {
const values = headers.map(header => {
const value = row[header];
// Handle nested objects and arrays
if (typeof value === 'object' && value !== null) {
return JSON.stringify(value);
}
// Escape commas and quotes
if (typeof value === 'string' && (value.includes(',') || value.includes('"'))) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
});
csvRows.push(values.join(','));
}
return csvRows.join('\n');
}
// Usage
const jsonData = [
{ name: "John", age: 30, city: "New York" },
{ name: "Jane", age: 25, city: "San Francisco" }
];
const csv = jsonToCsv(jsonData);
console.log(csv);
For more complex scenarios, check out libraries like json2csv:



