DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

Why split(',') Breaks CSV — and a Parser That Doesn't

"Just split(',') it." Famous last words. CSV is deceptively hard the moment a value contains a comma. Here's a CSV ⇄ JSON converter with a proper parser that handles quotes, runs entirely in the browser, no library.

🔄 Try it (both directions): https://dev48v.infy.uk/solve/day17-csv-to-json.html

Why split(',') is wrong

1,"Smith, John",30 has THREE fields, but split(',') gives you four. The comma inside the quotes isn't a separator. You need a parser that knows when it's inside a quoted field.

A stateful char-by-char parser

Walk the string one character at a time with an inQuotes flag:

  • a , while not in quotes → end of field
  • a " toggles inQuotes
  • a doubled "" inside quotes → a literal "
  • a newline while not in quotes → end of row

That handful of rules correctly parses commas-in-quotes, escaped quotes, and even newlines inside fields.

Both directions

CSV→JSON: header row becomes object keys. JSON→CSV: collect keys, and re-quote any field that contains a comma, quote, or newline.

The demo also handles delimiter choice (, ; tab) and "first row is header".

🔨 Full build (the quote-aware parse loop → rows→objects → JSON→CSV quoting) on the page: https://dev48v.infy.uk/solve/day17-csv-to-json.html

Part of SolveFromZero. 🌐 https://dev48v.infy.uk

Top comments (0)