DEV Community

GoToolsOnline
GoToolsOnline

Posted on

6 CSV edge cases that broke my "simple" CSV to JSON converter

I run a small free tools site, and this week I audited its CSV to JSON converter against what its help text promised. The help text said "RFC 4180" and "auto-detects delimiters". The code split the file on newlines and then on commas. Here is what broke, and what the rewrite does instead.

1. Line breaks inside quoted fields

name,note
Asha,"line one
line two"
Enter fullscreen mode Exit fullscreen mode

Splitting on \n first turns this into three broken rows. The fix is a small state machine that walks characters and only ends a row on a newline that is outside quotes.

2. Escaped quotes

In CSV a literal quote inside a quoted field is written twice: "He said ""hi""". A parser that just strips the outer quotes leaves you with He said ""hi"".

3. Spaces inside quotes are data

" Mumbai " should stay " Mumbai ". The old code trimmed every field, which silently changes values. Only trim when the user asks for it, and never inside quotes.

4. Not every "CSV" uses commas

European Excel exports often use semicolons; database dumps use tabs or pipes. Auto-detecting is simple if you count candidate delimiters on the first lines while ignoring characters inside quotes, then let the user override it.

5. Type conversion should be cautious

Converting "42" to 42 is helpful. Converting a PIN code or employee ID like 007 to 7 is data loss, and a 20-digit order ID turned into a JavaScript number loses its last digits (anything above 2^53 is not exact). My rule now: numbers only when they round-trip exactly, true/false/null as literals, everything else stays a string.

6. An unclosed quote should be an error, not a guess

If the file ends while still inside quotes, the old parser happily swallowed the rest of the file into one cell. Now it reports the problem, because a wrong JSON output that looks fine is worse than an error.

The other direction (JSON to CSV)

Two things matter most: nested objects need flattening (user.address.city as a column) and every field containing the delimiter, a quote, a line break or leading/trailing spaces must be quoted. If the CSV is going into Excel, writing a UTF-8 BOM keeps non-English text from turning into mojibake.

Takeaway

CSV looks trivial until real files arrive. If your tool's description promises a standard, write a test file for each edge case above and run it before you ship.

Disclosure: the converters I rewrote are on my site, GoToolsOnline. Both run in the browser, so the data isn't uploaded: https://gotoolsonline.com/tools/csv-to-json

Top comments (0)