DEV Community

Cover image for πŸ”„ CSV to JSON: Quoted commas, embedded newlines and CRLF handled properly
Tomaz
Tomaz

Posted on Originally published at tooladda.online

πŸ”„ CSV to JSON: Quoted commas, embedded newlines and CRLF handled properly

⚑ 10-second version

CSV looks trivial until a field contains a comma. Paste or drop your file at tooladda.online/csv-to-json-converter.html and get JSON that's correct on the awkward rows too β€” locally, so a customer export never leaves your machine.

❗ Important

CSV files are usually exports of real records: customers, orders, salaries, email lists. Parsed in your browser here. Nothing is uploaded, which is what makes it safe to convert a production export.


πŸ’£ Why split(',') is always wrong

Here is a perfectly legal CSV file per RFC 4180:

id,name,notes,city
1,"Sharma, Rahul","Said: ""call me Monday""",Delhi
2,"Iyer, Meera","Line one
Line two",Mumbai
Enter fullscreen mode Exit fullscreen mode

Look at what's actually in there:

Feature Where What naive parsers do
Comma inside a quoted field "Sharma, Rahul" split(',') gives you "Sharma and Rahul" β€” every column after it shifts by one
Escaped quotes as doubled quotes ""call me Monday"" Produces stray " characters or throws
A newline inside a quoted field Row 2's notes Split-by-line reads it as two broken rows
CRLF vs LF line endings Windows exports Leaves a trailing \r on the last value of every row β€” invisible, and it breaks every comparison

A correct parser is a small state machine that tracks whether it's inside quotes. That's what runs here, and it's why this handles the file above:

[
  { "id": 1, "name": "Sharma, Rahul", "notes": "Said: \"call me Monday\"", "city": "Delhi" },
  { "id": 2, "name": "Iyer, Meera",  "notes": "Line one\nLine two",      "city": "Mumbai" }
]
Enter fullscreen mode Exit fullscreen mode

Type inference: useful, and occasionally dangerous

CSV has no types β€” everything is text. Converting "42" to 42 is usually what you want, but automatic inference has famous failure modes:

CSV value Naive inference Problem
007 7 🚨 Leading zeros destroyed β€” fatal for PIN codes, phone numbers, employee IDs
+919876543210 919876543210 The + is gone; it's a phone number, not a quantity
2024-01-15 string or Date? Ambiguous β€” and 01/02/2024 is January 2nd or February 1st depending on locale
TRUE / yes / 1 true? Only if you know that column is a boolean
1E5 100000 Was it a product code?
(empty) "", null, or 0? Genuinely ambiguous β€” three different meanings

Which is why inference is a switch you control here rather than something applied silently. For anything with an identifier in it, keep the strings.


🧭 How it works

Diagram


✨ What's inside

### βœ… RFC 4180 parsing Quoted commas, doubled quotes, embedded newlines, CRLF. The four things that break every hand-rolled parser, handled. ### 🎚️ Type inference you control Turn it on for analytics data, off for anything with IDs, phone numbers or leading zeros. Your call, not a hidden default.
### πŸͺ† Nested key support Headers like `address.city` and `address.pin` become a nested object β€” useful when the JSON is going straight into an API. ### πŸ” Delimiter detection Comma, semicolon, tab or pipe. European exports are frequently semicolon-delimited because comma is their decimal separator.

πŸ› οΈ Real jobs

Situation What you do
🌱 Seeding a database or test fixture Turn a spreadsheet into a JSON array you can import.
πŸ”Œ Feeding an API Convert a client's CSV into the request body shape it expects.
πŸ“Š Charting library input Most JS charting libraries want JSON, not CSV.
πŸ§ͺ Mock data for a frontend Build sample data in a spreadsheet, ship it as JSON.
πŸ” Diagnosing a broken import See exactly where a malformed row goes wrong.
πŸ—ƒοΈ Migrating between systems CSV out of one, JSON into the next.

πŸ“– Four steps

1.  Open      β†’  tooladda.online/csv-to-json-converter.html
2.  Paste/drop→  your CSV
3.  Configure β†’  delimiter, header row, type inference on/off
4.  Copy      β†’  the JSON, or download it
Enter fullscreen mode Exit fullscreen mode

β–Ά Convert now β€” tooladda.online/csv-to-json-converter.html

πŸ’‘ Tip

Before converting a big file, check the first and last rows of the output. Header issues show up at the top, and a stray trailing newline or \r shows up at the bottom β€” those two spots catch most import failures.

⚠️ Warning

If your CSV has ID columns, PIN codes or phone numbers, turn type inference off for the whole file, or you'll silently lose leading zeros and + prefixes. That data does not come back.


❓ FAQ

Is it free? Is my data uploaded?

Free, no signup, and nothing is uploaded β€” parsing runs in your browser, which is why it's safe for real exports.

Does it handle commas inside quoted fields?

Yes, along with doubled quotes, embedded newlines and CRLF endings β€” the whole RFC 4180 set.

My phone numbers lost their leading zeros. Why?

Type inference converted them to numbers. Turn it off and they stay strings.

How large a file can it handle?

Limited by your device's memory rather than an upload cap. Files in the tens of megabytes are routine.

Can I get nested JSON?

Yes β€” dotted headers like user.address.city build nested objects.

My file is semicolon-separated. Is that valid?

Very common in locales where comma is the decimal separator. Pick the delimiter or let detection handle it.

Can I go the other way?

Yes β€” the JSON to CSV converter handles the reverse, including flattening nested objects.


πŸ”¬ Under the hood

  • Character-by-character state-machine parser in vanilla JavaScript β€” not a regex or a split.
  • Handles quote state, escaped quotes, embedded newlines and mixed line endings.
  • Type inference is explicit and reversible; strings are the safe default.
  • No upload endpoint exists; works offline once loaded.

Originally published on ToolAdda, where CSV to JSON runs free in your browser β€” nothing is uploaded, nothing leaves your device.

Top comments (0)