CSV to JSON
Processed in your browser · nothing is uploaded
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
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.
By doubling it. He said ""hi"" is one field containing He said "hi". CSV has no backslash escape.
Because a quoted field may contain a line break, so one record can span several lines. Addresses and free-text notes are where this shows up.
Nothing here: the header names are trimmed, which strips it. Left in place by a hand-rolled split it becomes part of the key, so the field looks exactly like name and every lookup on name misses it.
No, everything comes out as a string. Guessing types silently destroys leading-zero codes and long identifiers.
No, it reads commas. Excel writes the locale’s list separator, so an export from a Dutch, German or French machine is semicolon-separated because the comma is the decimal mark there; swap the semicolons for commas in find and replace before pasting it in.
The first row is used as keys regardless. Add a header row first if the data starts immediately.
A grid has no nesting. Flatten the document to dotted keys first, which the JSON formatter does, and each path becomes its own column.
No. It is parsed in the page, which is the point when the export is a list of customers with their addresses on it.