Developer Data

CSV to JSON

What to do

Processed in your browser · nothing is uploaded

Local · RFC 4180, not a comma split

Reads CSV the way RFC 4180 describes it rather than splitting on commas, so a quoted field holding a comma, a doubled quote or a line break arrives intact, and returns an array of objects keyed by the header row. Every value comes back as a string, because a CSV cell carries no type to read.

How to use the csv to json

1 Paste your input. The result appears immediately.
2 Pick the operation you want.
3 Copy the result, or save it as a file.

Splitting on commas is the mistake almost every quick script makes and it fails on the first address in the file. In RFC 4180 a field may be wrapped in double quotes, and inside those quotes a comma is data, a line break is data, and a quote is written by doubling it, "", because CSV has no backslash escape at all. The doubled quote is the detail hand-rolled parsers miss most often, and the embedded line break has a consequence worth holding on to: the number of lines in the file is not the number of records. A 500-row export with addresses in it can be 620 lines long, which is why a row count taken with wc -l disagrees with the spreadsheet.

The two things that go wrong before any parsing starts are both Excel’s doing. It writes the list separator from the operating system’s locale, so the same export is comma-separated on a machine set to English and semicolon-separated on one set to Dutch, German or French, where the comma is already the decimal mark. This page reads commas only, so a semicolon export arrives as a single column with the semicolons still inside it; replace them with commas first, in find and replace, and it parses. The other one it does handle for you: Excel puts a UTF-8 byte order mark in front of the first header, invisible in every editor, and left in place it becomes part of the first key so that every lookup on name misses a field that looks exactly like name. The header names are trimmed here, which takes it off.

Nothing is guessed at. Values stay strings because a CSV cell has no type information, and the alternative is worse than it sounds: a leading-zero postcode or SKU turns into an integer and loses its zeros, an identifier past 9007199254740991 loses its last digits, and something like 1-2 becomes a date in the wrong hands. Convert deliberately at the other end, where you know what a column is meant to be.

Going the other way is lossy for a different reason: CSV is flat and JSON is not. An object nested inside an object has nowhere to go in a grid, so flatten it to dotted keys first and the columns come out one level deep. The header is the set of keys the objects carry, so an object missing one leaves an empty cell rather than shifting the row, and any value containing the delimiter, a quote or a newline is quoted on the way out, which is the same rule that made the input readable in the first place.

What people use it for

  • Loading a spreadsheet export into an API that only takes JSON
  • Turning a customer list with quoted addresses into seed data
  • Getting an API response back into something a spreadsheet will open
  • Keeping leading-zero product codes intact across a conversion
  • Replacing the semicolons in a European Excel export so it parses as CSV

Questions

Because a quoted field can contain commas, quotes and line breaks. The first address in your file will break a naive split.

RFC 4180; common format for CSV files
Was this tool any good?
Internal signal only · I use it to find the tools worth rebuilding