How to Convert CSV to JSON
CSV and JSON are the two formats data actually travels in. CSV comes out of spreadsheets and databases; JSON is what APIs and applications want. Converting between them looks like a five-minute job — split on commas, done — and that naive approach is exactly why so much data arrives quietly corrupted. This guide covers the mapping, the traps, and the Excel behaviours that have destroyed a genuinely famous amount of data.
The mapping
The conversion is conceptually clean. A CSV file has a header row naming the columns, and then one row per record:
name,age,city
Alice,30,Dhaka
Bob,25,Tokyo
Each row becomes a JSON object, with the header names as keys. All the rows together become an array:
[ { "name": "Alice", "age": 30, "city": "Dhaka" }, { "name": "Bob", "age": 25, "city": "Tokyo" } ]
That array-of-objects shape is what essentially every API expects, which is why this conversion is so common.
Why splitting on commas breaks
Here is the first and most important trap. What happens when the data itself contains a comma?
name,city"Smith, John",Dhaka
Split that on commas and you get three fields — "Smith, John" and Dhaka — for a two-column table. The row is now misaligned, and every value after it lands in the wrong column.
CSV solves this with quoting: a field containing a comma is wrapped in double quotes, and commas inside those quotes are data, not separators. Which means a correct parser must track whether it is currently inside a quoted field.
And it goes further:
- Quotes inside quoted fields are doubled. A value of
He said "hi"is written"He said ""hi""". - A quoted field may contain a line break. This is the one that really hurts — a single CSV record can span multiple lines of the file. So you cannot even split the file into rows by newline first. An address field with a line break in it will silently split one record into two.
The lesson: never write your own CSV parser. It looks trivial and it is not. Use a proper parser that handles quoting and embedded newlines.
The delimiter is not always a comma
Despite the name, plenty of “CSV” files are not comma-separated. In countries where the comma is the decimal separator — much of Europe — a comma cannot also separate fields, so Excel uses a semicolon instead. Tab-separated files are also common.
So a file that opens perfectly on one colleague's machine can arrive as a single mangled column on yours. If a CSV looks wrong, check the delimiter before anything else.
Types: everything in CSV is a string
This is the conceptual gap between the formats. CSV is plain text and has no types. JSON has six. So the converter has to guess.
Should 30 become the number 30 or the string "30"? Usually the number. But consider:
- Phone numbers.
0171234567converted to a number becomes171234567— the leading zero is gone, and the number is now wrong. - Postcodes and ZIP codes. Same problem.
01234becomes1234. - IDs and product codes. Should stay strings, always.
- Very long numbers. Anything beyond about 16 digits loses precision when treated as a number. Credit-card-length numbers get silently altered.
The rule: if it is an identifier rather than a quantity, keep it a string. You never do arithmetic on a phone number.
The Excel traps
Excel is where most CSVs come from, and it damages data in ways people do not notice until much later.
- It eats leading zeros. Open a CSV with a ZIP code of
01234and Excel helpfully converts it to1234. Save the file and the correct value is gone permanently. - It turns things into dates. A value like
3-4becomes 3 April. A gene name likeSEPT2becomes September 2nd. This is not a hypothetical: it corrupted a significant fraction of published genomics papers, and geneticists eventually renamed several human genes so Excel would stop mangling them. Excel won. - It applies scientific notation. Long numeric IDs become
1.23457E+14, losing digits permanently. - It guesses encoding. Non-English characters routinely arrive as garbled text because Excel and the file disagree about UTF-8.
The defence is simple: do not open a CSV in Excel just to look at it.Opening and saving is enough to corrupt it. Use a text editor, or a proper import step where you explicitly set each column's type to text.
Nested data: where CSV runs out
CSV is flat — a rectangle of rows and columns. JSON is hierarchical, and can nest objects and arrays freely.
So converting CSV to JSON always produces a flat structure. There is no way to express “this user has three addresses” in a CSV row without a convention — such as columns named address_1, address_2, or a delimiter inside a single cell.
Going the other way, from JSON to CSV, is genuinely lossy: nested structures have to be flattened, and there is no single correct way to do it. If your JSON is deeply nested, CSV is simply the wrong destination.
Common mistakes to avoid
- Splitting on commas instead of using a real parser.
- Letting Excel touch the file before conversion.
- Converting identifiers to numbers and losing leading zeros or precision.
- Assuming the delimiter is a comma.
- Ignoring the encoding. Specify UTF-8 explicitly.
- Trusting an empty cell. Is it an empty string, a null, or a zero? CSV cannot tell you, and the converter has to choose.
Frequently asked questions
Why does my CSV have extra columns after conversion? Almost certainly unquoted commas inside a data field.
Why did my leading zeros disappear? The value was treated as a number. It should be a string.
Can I convert JSON back to CSV? Yes, if the JSON is flat. Nested structures must be flattened, which loses information.
Is it safe to convert a customer file online?Only if the tool runs in your browser. A customer list uploaded to a stranger's server is a data-protection incident.
Convert CSV to JSON now
Use our CSV to JSON Converter — it handles quoted fields properly and runs entirely in your browser, so customer data never leaves your device. Tidy the result with the JSON Formatter, and read what JSON is and how to format it.