Back to Blog
·7 min read

How to Convert Between JSON and CSV (Without Losing Data)

A practical guide to converting JSON arrays to CSV and back — covering nested data, type preservation, and the pitfalls that cause silent data loss.

JSON and CSV solve very different problems — JSON represents nested, typed, hierarchical data; CSV represents flat, tabular rows and columns. Converting between them is one of the most common data-wrangling tasks developers face, whether you're exporting API data to a spreadsheet for a stakeholder or importing a CSV export into a JSON-based pipeline. Here's how to do it correctly — and what can silently go wrong.

Why convert JSON to CSV?

CSV is the lingua franca of spreadsheets — Excel, Google Sheets, and virtually every BI and reporting tool can open it directly. If you need to hand data to someone who lives in spreadsheets (which, in most organizations, is most people), CSV is the path of least resistance.

[
  { "id": 1, "name": "Alice", "role": "admin" },
  { "id": 2, "name": "Bob", "role": "editor" }
]
id,name,role
1,Alice,admin
2,Bob,editor

The challenge: nested data has no CSV equivalent

CSV is fundamentally two-dimensional — rows and columns. JSON is not. The moment your objects contain nested objects or arrays, there's no lossless, universal way to represent that in a flat table. Converters typically handle this in one of two ways:

  • ·Serialize nested values as JSON strings within the cell — preserves all data, but the cell content isn't directly usable in spreadsheet formulas
  • ·Flatten nested keys into separate columns using dot notation (e.g., address.city, address.zip) — produces clean tabular columns, but can create very wide tables for deeply nested data

If you need flat columns from nested JSON, flatten the structure first — this gives you full control over how the hierarchy maps to column names before converting to CSV.

JSON Flattener

Convert nested JSON into flat dot-notation keys (e.g., address.city) — ideal preparation before exporting to CSV or a spreadsheet.

JSON to CSV Converter

Converts a JSON array of objects to a downloadable CSV file with automatic header detection and proper escaping.

Why convert CSV to JSON?

The reverse direction is just as common — importing a spreadsheet export into an application, a database seed script, or an API payload. The main job here is mapping each row to an object using the header row as keys.

name,age,city
Alice,30,Austin
Bob,25,Denver
[
  { "name": "Alice", "age": "30", "city": "Austin" },
  { "name": "Bob", "age": "25", "city": "Denver" }
]

Tip: Notice that "age" became the string "30", not the number 30 — CSV has no concept of data types, so every value parses as a string by default. If you need numbers or booleans, you'll need to convert types after parsing.

Pitfalls that cause silent data loss

Delimiter mismatches

Not all CSVs use commas. Files exported from European locales of Excel commonly use semicolons (because commas are decimal separators there), and some exports use tabs. If you parse a semicolon-delimited file as comma-delimited, you'll get one giant column instead of several — a converter that auto-detects the delimiter avoids this entirely.

Values containing commas, quotes, or newlines

A value like "Smith, John" or a multi-line address must be quoted in CSV, and embedded quotes must be escaped by doubling them ("" inside a quoted field). Naive splitting on commas breaks immediately on real-world data — always use a proper CSV parser that understands quoting rules.

Inconsistent keys across JSON objects

If some objects in your array have keys that others don't, the resulting CSV needs a column for every key that appears anywhere — with empty cells for objects missing that key. A good converter handles this by scanning the full array for the union of keys before generating headers.

CSV to JSON Converter

Paste or upload a CSV and get a clean JSON array — auto-detects delimiters and handles quoted fields correctly.

Quick checklist before converting

  • ·Decide how to handle nested objects/arrays before converting JSON → CSV (flatten, or accept embedded JSON strings)
  • ·Check whether your CSV uses commas, semicolons, or tabs as the delimiter
  • ·Remember that everything coming out of CSV → JSON is a string — convert types explicitly where needed
  • ·Watch for inconsistent columns/keys across rows or objects
  • ·Always inspect a sample of the converted output before processing the full dataset
Ad