All tools run in your browser — your files never leave your device.
All tools154

Developer & Security

CSV to JSON: the five things that go wrong

Every CSV parser written in an afternoon splits on commas. Every one of them breaks on the first address field.

The short answer. CSV to JSON conversion fails in five predictable places: commas inside quoted fields, embedded line breaks, numbers that must stay strings (leading zeros, phone numbers, IDs), inconsistent row lengths, and character encoding. A correct converter follows RFC 4180 quoting rules rather than splitting on commas, and lets you decide which columns keep their string type.

Commas inside fields

This is the first thing to break and the reason naive parsers cannot be trusted.

CSV allows a field to contain the delimiter as long as the field is quoted. So this row has three fields, not four:

1,"Smith, John",London

A parser that splits on commas produces four values and silently shifts every column after it. Nothing errors — you just get an object where the city is John" and the data is quietly wrong.

A quote inside a quoted field is escaped by doubling it, so "She said ""hello""" is one field reading She said "hello". Any parser that does not implement that rule will mangle quoted speech, inch marks and anything with an apostrophe convention that uses double quotes.

Line breaks inside fields

A quoted CSV field can contain actual newlines. A multi-line address in one cell is completely legal:

1,"221B Baker Street
London
NW1 6XE",UK

This defeats the other common shortcut — reading the file line by line. One record now spans three lines, and a line-based reader produces three broken records from it.

This is why CSV has to be parsed character by character with a state machine tracking whether you are inside quotes. It is not a format you can split your way through, however much it looks like one.

Numbers that must not become numbers

JSON distinguishes "01234" from 1234. CSV does not. A converter that helpfully detects numeric columns will destroy several kinds of data:

Numbers that must not become numbers
CSV valueNaive resultShould be
012341234"01234"
+44 20 7946 095844207946095"+44 20 7946 0958"
00723723"00723"
1.101.1"1.10" if it is a version
3E2300"3E2" if it is a product code
000000000000001212"0000000000000012"

Postcodes, phone numbers, product SKUs, account numbers, ISBNs and anything with a leading zero all need to stay strings. This is the same failure that makes spreadsheets turn gene names into dates — automatic type detection is a guess, and it guesses wrong on identifiers.

Ragged rows and missing headers

Real CSV files are rarely rectangular. Rows come up short, come up long, carry trailing empty columns from a spreadsheet export, or repeat the header partway through because two exports were concatenated.

A converter has to decide what to do, and the right answer depends on your data:

  • Short rows — fill missing keys with null, or omit them. Omitting is smaller; filling is easier to consume, because every object then has the same shape.
  • Long rows — usually a quoting bug upstream rather than real data. Worth surfacing as an error rather than silently dropping the extras.
  • Duplicate headers — two columns both called name cannot both be a key. One will overwrite the other.
  • Empty trailing columns — a spreadsheet artifact. Strip them before converting or every object carries a stray empty field.

If the file is messy enough that rows repeat or blank lines litter it, clean it first — Duplicate Line Remover and Text Cleaner handle that faster than fixing the JSON afterwards.

Encoding, BOMs and delimiters that are not commas

Three smaller traps that account for most of the remaining failures.

Encoding. A file saved as Windows-1252 and read as UTF-8 turns every accented character into mojibake. Excel on Windows still exports in the system codepage unless you explicitly choose UTF-8.

The byte order mark. A UTF-8 BOM at the start of the file becomes part of the first header name, so your first key is invisibly id rather than id and every lookup for id fails. This one costs people hours because the two strings look identical in every log.

The delimiter. In locales where the comma is the decimal separator, "CSV" files are semicolon-delimited. Tab-separated files are also routinely named .csv.

A converter worth using detects the delimiter, strips the BOM, and tells you which encoding it assumed. The CSV to JSON tool does the parse locally, which matters when the CSV is a customer export — that is exactly the file you should not be pasting into a random web form.

Frequently asked questions

Why does my CSV break when a field contains a comma?

Because the parser is splitting on commas rather than following CSV quoting rules. A quoted field may contain the delimiter — "Smith, John" is one field, not two. Nothing errors; every column after it just shifts silently, which is why the bug is often found weeks later.

How do I stop leading zeros being stripped?

Keep the column as a string rather than letting the converter detect it as numeric. Postcodes, phone numbers, SKUs, account numbers and ISBNs all need this. Automatic type detection is a guess and it guesses wrong on anything that is an identifier rather than a quantity.

What is the BOM problem in CSV files?

A UTF-8 byte order mark at the start of the file gets absorbed into the first header name, so your first key is invisibly different from what you type in code. Lookups fail and the two strings look identical in every log. A good parser strips it.

Can a CSV field contain a line break?

Yes, inside quotes — multi-line addresses are common and completely legal. This is why CSV cannot be parsed line by line: one record can span several lines, and a line-based reader turns it into several broken records.

What if my rows have different numbers of columns?

Decide deliberately. Short rows can be filled with null or have keys omitted — filling is easier to consume because every object then has the same shape. Long rows usually indicate a quoting bug upstream and are worth surfacing as an error rather than silently truncating.

My CSV is semicolon-separated. Is that still CSV?

In practice yes. Locales where the comma is the decimal separator export with semicolons, and tab-separated files are routinely named .csv too. A converter should detect the delimiter rather than assume a comma.

Stop reading, start doing

Every tool in this guide is free.

154 browser-based utilities. No account, no upload, and no file size limit — your files are processed on your own device and never sent anywhere.

Browse all 154 tools