CSV vs JSON: Differences and How to Convert Between Them
CSV is a flat table of rows and columns, ideal for spreadsheets. JSON stores structured, nested data with types, ideal for APIs and apps. To convert CSV to JSON, each row becomes an object keyed by the header row; to convert JSON to CSV, nested objects are flattened into columns like “address.city”.
Side-by-side comparison
| CSV | JSON | |
|---|---|---|
| Structure | Flat rows and columns | Nested objects and arrays |
| Data types | Everything is text | Strings, numbers, booleans, null |
| Best for | Spreadsheets, exports, bulk imports | APIs, configuration, web apps |
| Size | Compact – headers once | Larger – keys repeat per record |
| Standard | RFC 4180 (loosely followed) | RFC 8259 (strict) |
Converting CSV to JSON
With a header row, “name,age / Ana,31” becomes [{"name":"Ana","age":31}] when type detection is on. Without it, everything stays a string: "31". Values with leading zeros such as postal codes “01234” should stay strings, otherwise they change meaning.
Converting JSON to CSV
Because CSV is flat, nested objects must be flattened. {"user":{"id":1,"city":"Oslo"}} becomes two columns: user.id and user.city. Arrays have no natural column form, so they are usually kept as JSON text inside one cell.
Checklist before converting
- Validate the JSON first – a single trailing comma makes the whole file invalid.
- Check your CSV delimiter: many European systems use semicolons.
- Decide how to treat empty cells: empty string or null.
- Keep IDs longer than 15 digits as text; spreadsheets round them.
Questions and answers
Which is smaller, CSV or JSON?
CSV is usually smaller because column names appear once in the header, while JSON repeats each key in every record.
Can CSV store nested data?
Not natively. Nested values must be flattened into extra columns (such as address.city) or stored as JSON text inside a cell.