CSV and JSON: How to Convert Between the Two Most Common Data Formats
CSV and JSON are the two most common data exchange formats. CSV is the lingua franca of spreadsheets and databases. JSON is the standard for APIs and web applications. Converting between them is a daily task for developers, analysts, and data scientists.
When to Use Each Format
| Aspect | CSV | JSON |
|---|---|---|
| Best for | Tabular data, spreadsheets, bulk import | APIs, config files, nested data |
| Structure | Flat rows and columns | Nested objects and arrays |
| Human-readable | Yes (simple data) | Yes (with formatting) |
| File size | Smaller (no keys repeated) | Larger (keys per object) |
Common Conversion Pitfalls
- Commas inside values — Must be quoted:
"Smith, John",35 - Newlines in fields — Must be quoted; many parsers break on these
- Different delimiters — European CSVs use semicolons; TSVs use tabs
- Nested JSON — Flattening nested objects into CSV columns requires decisions about naming
- Encoding — Always use UTF-8; Excel sometimes defaults to locale-specific encodings
Convert your Data
Use our CSV to JSON Converter to convert bidirectionally with auto-delimiter detection, quoted field handling, and proper escaping.
Worked Example: Converting a Sales Export to JSON
Say your CRM exports a CSV of deals that a REST API must consume. One row reads "Acme, Inc.",EMEA,12000 — the company name contains a comma, so it must stay quoted. A correct converter produces:
[
{"name": "Acme, Inc.", "region": "EMEA", "deal_size": 12000},
{"name": "Initech", "region": "NA", "deal_size": 4200}
]
The steps a reliable conversion follows:
- Split rows on commas that are not inside quotes — this is where naive
split(",")code breaks and produces ragged objects - Strip surrounding quotes and unescape doubled quotes (
""becomes a single") - Cast numeric columns:
12000becomes the number 12000, not the string"12000" - Emit one JSON object per row, using the header row as keys
Going the other way, JSON to CSV, is easy for flat objects but needs decisions for nested ones — flatten {"address": {"city": "Oslo"}} into a column named address.city, for example, and apply the same rule to every row.
Common Mistakes
- Splitting on commas blindly —
text.split(',')destroys any field containing a comma, silently shifting every value after it one column to the left. Use a parser that understands quoted fields. - Losing leading zeros — zip codes like
02134become the number 2134 in Excel and in careless JSON casts. Keep them as strings end to end. - Ignoring BOM and encoding — Excel exports often start with a UTF-8 BOM or use Windows-1252, turning é into é downstream. Always normalize to clean UTF-8 before converting.
- Assuming every row has the same columns — real exports have missing and extra fields. Decide up front whether missing keys become
null, an empty string, or are omitted. - Letting Excel reformat on open — opening a CSV in Excel can turn 1-2 into the date January 2. Inspect exports in a text editor before trusting what the spreadsheet shows.
Frequently Asked Questions
Which format should my API return?
JSON for anything consumed by code — it is self-describing and nests naturally. Offer CSV only when the consumer is explicitly a spreadsheet, such as an admin "Export" button built for finance teams.
How do I handle nested JSON when converting to CSV?
Flatten each nested object into dot-notation column names like address.city, or serialize the nested part as a JSON string inside a single cell. Choose one convention and document it, because there is no standard.
What delimiter should I use?
Commas for anything shared internationally; semicolons where European Excel is the primary consumer; tabs (TSV) when fields may contain both commas and quotes. A good converter detects the delimiter automatically.
Is there a size limit for conversion?
Practical limits come from memory, not the formats. Millions of rows of CSV stream fine, but the JSON equivalent can be several times larger because every object repeats its keys. Convert very large files in chunks when you can.
The Bottom Line
- Use CSV for flat tabular data and spreadsheet exchange
- Use JSON for APIs, configuration, and nested structures
- Always handle quoted fields and special characters when converting
- Use UTF-8 encoding to avoid character corruption
Disclaimer: This guide is for informational purposes only.
Related Free Tools
Put this guide into practice with our free browser-based tools — no signup, no upload, 100% local processing.