What Is JSON/CSV Conversion
JSON/CSV conversion is the process of converting between JSON arrays (object lists) and CSV table format. JSON suits programmatic processing; CSV suits Excel and analytics tools.
Conversion Example
JSON → CSV:
[
{ "name": "Alice", "age": 28, "city": "Shanghai" },
{ "name": "Bob", "age": 32, "city": "Beijing" }
]
CSV Output:
name,age,city
Alice,28,Shanghai
Bob,32,Beijing
JSON vs CSV Use Cases
| Scenario | JSON | CSV | |----------|------|-----| | Web API | ✅ Preferred | ❌ Not suitable | | Excel processing | ❌ Not native | ✅ Native support | | Nested data | ✅ Supported | ❌ Not supported | | Data analysis | ⚠️ Needs parsing | ✅ Direct analysis | | Data export | ⚠️ Not intuitive | ✅ User-friendly | | Config files | ✅ Suitable | ❌ Not suitable | | Large datasets | ⚠️ Larger files | ✅ Smaller files |
Why You Need JSON/CSV Conversion
- Data analysis: Import API JSON data into Excel/Google Sheets
- Data export: Export database query results (JSON) as CSV downloads
- Data migration: Migrate data between different systems
- Report generation: Convert JSON data to CSV for reports
- Batch operations: Edit in Excel, export back to JSON
- Data visualization: Many tools accept CSV input
Conversion Challenges
Flattening Nested JSON
JSON supports nesting; CSV doesn't. You need to "flatten" nested objects:
// Original
{ "name": "Alice", "address": { "city": "Shanghai", "zip": "200000" } }
// Flattened
{ "name": "Alice", "address.city": "Shanghai", "address.zip": "200000" }
Array Field Handling
JSON arrays need special handling in CSV:
{ "name": "Alice", "tags": ["developer", "admin"] }
Common solutions: join with delimiter (developer;admin) or split into multiple rows.
How to Use an Online Tool
Using DevToolkit Pro's JSON/CSV Converter:
- Paste JSON array or CSV data
- The tool auto-detects format and converts
- Supports nested JSON flattening
- Custom delimiters and quote handling
- Preview and copy results
Conversion in Code
JavaScript: JSON to CSV
function jsonToCSV(jsonArray) {
if (!jsonArray.length) return '';
const flatten = (obj, prefix = '') =>
Object.entries(obj).reduce((acc, [key, val]) => {
const fullKey = prefix ? `${prefix}.${key}` : key;
if (typeof val === 'object' && val !== null && !Array.isArray(val)) {
Object.assign(acc, flatten(val, fullKey));
} else {
acc[fullKey] = Array.isArray(val) ? val.join(';') : val;
}
return acc;
}, {});
const flatData = jsonArray.map(item => flatten(item));
const headers = Object.keys(flatData[0]);
const rows = flatData.map(row =>
headers.map(h => {
const val = String(row[h] ?? '');
return val.includes(',') || val.includes('"') || val.includes('\n')
? `"${val.replace(/"/g, '""')}"` : val;
}).join(',')
);
return [headers.join(','), ...rows].join('\n');
}
JavaScript: CSV to JSON
function csvToJSON(csv) {
const lines = csv.trim().split('\n');
const headers = lines[0].split(',').map(h => h.trim());
return lines.slice(1).map(line => {
const values = line.split(',');
return headers.reduce((obj, header, i) => {
let val = values[i]?.trim() ?? '';
if (val === 'true') val = true;
else if (val === 'false') val = false;
else if (val === 'null') val = null;
else if (!isNaN(val) && val !== '') val = Number(val);
obj[header] = val;
return obj;
}, {});
});
}
FAQ
How to Convert Nested JSON to CSV?
You need to "flatten" nested objects first. Common approaches: dot notation (address.city) or underscore (address_city). Online tools typically handle flattening automatically.
Why Does CSV to JSON Lose Number Types?
CSV has no type info — all values are strings. Add type inference during conversion: detect numeric strings → Number, true/false → boolean.
How to Handle Nested Arrays in JSON?
JSON arrays can contain other arrays or objects. For CSV conversion, decide: flatten to multiple rows (one per element) or merge into one field (delimiter-separated). Depends on your use case.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
JSON Parse Error 'Unexpected token'? 5 Common Causes and How to Fix Them
Getting a JSON parse error 'Unexpected token'? This article covers the 5 most common causes of JSON format errors — trailing commas, single quotes, unescaped characters, BOM headers, and comments — with fixes and code examples for each.
Complete Guide to JSON Schema: From Beginner to Practical
Deep dive into JSON Schema core concepts, use cases, and best practices. Learn how to write and auto-generate JSON Schema, and master JSON data validation techniques.
Best Online JSON Formatter Tools Comparison (2026)
Comparing 5 popular online JSON formatter tools across features, speed, privacy, and pricing. DevToolkit Pro stands out with local processing, Unicode safety, and free unlimited use.