elefcode
← All guides

Web & APIs · 4 min read

CSV vs JSON: Converting Between Tables and Objects

CSV and JSON are the two formats data most often travels in. CSV is what spreadsheets export; JSON is what APIs return. Converting between them is a daily task — and the places it goes wrong are predictable.

Try it yourself with the related tool.

Convert JSON and CSV →

Advertisement

Different shapes of data

CSV is a flat table: rows and columns, with every value a string. JSON is a tree: objects, arrays, numbers, booleans, and null, nested as deeply as you like. Converting from JSON to CSV therefore means deciding how to squash a tree into a grid; going the other way means guessing types that CSV never recorded.

Flattening nested objects

The usual approach is dot-notation column names: {"user": {"name": "Ana"}} becomes a user.name column. Arrays are harder — you can join them into one cell, spread them into numbered columns like tags.0 and tags.1, or create one row per array item. Choose based on what the person opening the spreadsheet needs.

Type and format pitfalls

When reading CSV back into JSON, "00123" may become the number 123 and lose its leading zeros, and long IDs can lose precision once parsed as numbers. Spreadsheets also convert values that look like dates. Keep identifiers as strings, and be explicit about which columns are numbers.

Which to use

Use CSV for flat, tabular data that people will open in Excel or Google Sheets, or for large bulk imports where its compactness helps. Use JSON for APIs, configuration, and anything with nesting or mixed types.

Related guides