CSV and JSON are two of the most common data formats you’ll encounter. Knowing how to convert between them — and when each format makes sense — is a useful skill for developers, data analysts, and anyone who works with data.
CSV: simple and tabular
CSV (Comma-Separated Values) is plain text where each line is a row and values are separated by commas:
name,age,city
Alice,30,New York
Bob,25,London
Carol,35,Tokyo Strengths: Human-readable, tiny file sizes, opens in any spreadsheet app (Excel, Google Sheets), easy to edit manually.
Weaknesses: No nested data, no data types (everything is a string), delimiter conflicts (what if a value contains a comma?).
JSON: structured and flexible
JSON (JavaScript Object Notation) represents the same data with explicit structure:
[
{ "name": "Alice", "age": 30, "city": "New York" },
{ "name": "Bob", "age": 25, "city": "London" },
{ "name": "Carol", "age": 35, "city": "Tokyo" }
] Strengths: Supports nested objects and arrays, preserves data types (numbers, booleans, null), native to JavaScript and most APIs.
Weaknesses: Larger file sizes, harder to edit in a spreadsheet, more verbose.
When to use which
| Scenario | Use |
|---|---|
| Spreadsheet import/export | CSV |
| API request/response | JSON |
| Database export for analysis | CSV |
| Configuration files | JSON |
| Simple flat data | CSV |
| Nested or complex data | JSON |
| Sharing with non-technical users | CSV |
Converting CSV to JSON
The conversion process:
- Parse the header row — The first line defines the field names
- Parse each data row — Split by delimiter (comma, tab, semicolon, etc.)
- Build objects — Map each value to its corresponding header
- Handle edge cases — Quoted values, escaped delimiters, empty fields
Watch out for:
- Delimiter conflicts — Values containing commas should be wrapped in quotes
- Line breaks in values — Quoted fields can contain newlines
- Encoding — UTF-8 is standard, but some files use other encodings
- Empty values — Decide whether they should be empty strings, null, or omitted
Converting JSON to CSV
Going the other direction:
- Extract keys — Use the keys from the first object as column headers
- Flatten nested data — Nested objects need to be flattened or stringified
- Escape special characters — Wrap values containing delimiters in quotes
- Write rows — Output each object as a comma-separated line
Common pitfalls:
- Nested objects — CSV is flat, so
{ address: { city: "NYC" } }needs to becomeaddress.cityor be serialized - Arrays in values — No standard way to represent arrays in CSV
- Inconsistent keys — If objects have different keys, you need to handle missing values
Custom delimiters
Not all “CSV” files use commas. Common alternatives:
- Tab (TSV) — Common in database exports
- Semicolon — Standard in European locales where comma is the decimal separator
- Pipe (
|) — Used when data contains commas and semicolons
When converting, make sure to detect or specify the correct delimiter.
Browser-based conversion
For quick conversions, browser-based tools are the fastest option. Paste your CSV or JSON, and get the converted output instantly. No uploading, no installing, no waiting. The conversion happens entirely in your browser using JavaScript.