CSV to JSON
CSV to JSON, quoting handled properly.
CSV to JSON, quoting handled properly.
How to use it
- Paste the CSV. The first row is used as the keys.
- Pick the separator. Semicolons where a comma is the decimal separator.
- Decide about numbers. Off keeps everything as text, which is the safe default.
When you would use this
Converting CSV is the classic example of a problem that looks like one line of code and is not. Splitting on commas works until a field contains one, which in real data is immediately: the first customer whose name is written surname first breaks the row. A correct parser has to read the file character by character, because a quoted field can contain the delimiter, a doubled quote and a line break, and a line based split cannot see any of them. That is what this does, following RFC 4180. Numbers are left as text unless you ask, and that is deliberate. Conversion is lossy and irreversible: once an account number, a postcode or a phone number has become a number, the leading zeros are gone and JSON cannot tell you they were ever there. Even with conversion on, anything with a leading zero stays text for exactly that reason. A row with the wrong number of cells is converted rather than rejected, with the count reported, since a single ragged row in a thousand should not cost you the other nine hundred and ninety nine.
Questions
- Does it handle commas inside a field?
- Yes, and quotes and newlines too. It reads the file character by character rather than splitting on lines and commas, which is the only way a quoted field containing a delimiter, a doubled quote and a line break can survive. Splitting works right up until real data arrives.
- Why are numbers text by default?
- Because converting is lossy and you should choose it. An identifier made of digits is not a quantity, and JSON cannot tell them apart once converted. Even with converting on, anything with a leading zero stays text, since turning an account number like 007 into 7 destroys it irreversibly.
- What if a row has the wrong number of cells?
- It is still converted, missing cells become empty strings, and the count of ragged rows is reported. Refusing the whole file for one bad row would be less useful than converting it and saying so.