Converting CSV to JSON Without Breaking Quoted Fields
Splitting on commas works until a value contains a comma. Here is what a real CSV parser handles that a split() does not.
The naive CSV parser is one line:
line.split(',')
It works on your sample file and fails in production. Here's everything it misses.
Commas inside values
name,address,city
Ada Lovelace,"12 Bridge St, Flat 4",London
That address is one field. split(',') gives you four columns instead of three and silently shifts every value after it. Nothing errors — you just get wrong data.
Quotes inside quoted values
CSV escapes a quote by doubling it:
quote
"She said ""hello"" twice"
The value is She said "hello" twice. A naive parser produces something with stray quote characters in it.
Line breaks inside values
A quoted field can contain a newline. This means you cannot split a CSV file into records by splitting on \n — a multi-line address or a comment field will be torn in half.
Delimiters that aren't commas
European exports frequently use semicolons, because comma is the decimal separator there. Tab-separated files are common out of databases. A converter should detect the delimiter rather than assume.
The BOM
Excel writes UTF-8 files with a byte-order mark. Read it naively and your first column header becomes \uFEFFname instead of name — so lookups by name return undefined, on the first column only, for no visible reason.
Type detection is a choice, not a default
Should "42" become the number 42? Should "true" become a boolean? Usually yes — but not for a column of postcodes with leading zeros, or IDs long enough to lose precision as a JavaScript number. Any converter that does this silently will eventually corrupt something.
Doing it properly
SwitchPDF CSV to JSON handles quoted fields, doubled quotes, embedded newlines, custom delimiters and the BOM, and lets you decide whether to infer types. It converts in both directions and runs in your browser, so a customer export never leaves your machine.
Checking your output
Two quick checks catch most problems:
- Row count — the JSON array length should equal the CSV's data rows. If it's higher, an embedded newline split a record.
- Spot-check the messiest row — find a value containing a comma or a quote and confirm it survived intact.
Bottom line
CSV looks like the simplest format in the world and isn't. If your data has any free-text field in it, use a real parser — the failure mode is silently wrong data rather than an error, which is the worst kind.
Related articles
UUID v4 vs v7: Which One to Use as a Database Key
Random UUIDs fragment your index. v7 fixes it by putting a timestamp at the front. Here is the trade-off.
Base64 Explained: When to Use It and When Not To
Base64 is not encryption and it makes your data bigger. Here is what it is actually for.
Converting JSON to YAML for Kubernetes and Docker Compose
Your API speaks JSON, your infrastructure speaks YAML. Converting between them has three gotchas worth knowing about.