CSV to JSON converter
Type or paste into either box, and the other one updates as you go. The converter reads quoted fields the way RFC 4180 describes and keeps every CSV value as a JSON string, so 007 stays "007". It runs in your browser; nothing you enter is uploaded.
How CSV quoting works
The quoting rules come from RFC 4180, which wrote down the CSV format in October 2005:
- Commas separate fields, and line breaks separate records.
- A field that contains the delimiter, a double quote, or a line break goes inside double quotes.
- Inside a quoted field, each double quote is written twice.
So each row of this CSV has three fields, even though the second row has a comma and quotes inside its fields and the third has a line break:
id,name,note
1,"Smith, Jane","She said ""hi"""
2,Émile,"Two
lines" With "First row is the header" ticked, the converter turns it into:
[
{
"id": "1",
"name": "Smith, Jane",
"note": "She said \"hi\""
},
{
"id": "2",
"name": "Émile",
"note": "Two\nlines"
}
]
The RFC only covers commas, so the semicolon and tab options use the same quoting with a different delimiter. The converter reads LF, CRLF,
and CR line breaks, skips empty lines, and drops the byte order mark Excel puts at the start of files saved as "CSV UTF-8". A quote in the
middle of an unquoted field, as in 5" screen, stays as an ordinary character. But text right after a closing quote is an
error, because there's no telling where that field was meant to end.
Headers, uneven rows, and types
With "First row is the header" ticked, each row after the first becomes an object whose keys are the header's names. Untick it to get an
array of arrays instead, with the first row as the first array. If a header cell is empty, the converter names the column after its
position, such as column3. A repeated name gets a number, so two name columns become name and
name_2.
When a row has fewer fields than the widest row, the converter fills the missing ones with empty strings, and a note under the boxes tells you how many rows didn't match the header.
Every value stays a string. CSV has no types, and guessing them changes your data: the ZIP code 02134 would become the number
2134, and an ID such as 1e5 would become 100000. Convert the columns you know are numbers in your own code.
JSON to CSV
Paste an array of objects, an array of arrays, or a single object. For objects, the converter builds the header from every key that appears
in any of them, in the order the keys first appear. An object that lacks one of those keys gets an empty field in that column, and so does a
null. A nested object or array goes into one cell as its JSON text. The converter quotes a field only when it needs quotes.
The CSV uses LF line breaks. RFC 4180 asks for CRLF, but Python's csv module, Excel, and LibreOffice read either. JSON numbers
go through JavaScript's JSON.parse, so integers above 253 lose precision:
12345678901234567890 comes out as 12345678901234567000. Keep IDs that long as strings in the JSON.
Opening the CSV in a spreadsheet
When Excel opens a .csv file, it splits the columns on the list separator from the computer's regional settings. In countries that write decimals with a comma, such as Germany and France, that separator is a semicolon, so a comma-separated file opens with each whole row crammed into column A. If the file is going to readers there, choose Semicolon.
Spreadsheet apps also treat a cell that starts with =, +, -, or @ as a formula. If the
data came from other people, that opens the door to
CSV injection: someone plants a cell that runs a formula
on the computer of whoever opens the file. Before you export, put an apostrophe in front of such cells or strip those characters.
On the command line
mlr --icsv --ojson cat data.csv
jq -r '(.[0] | keys_unsorted) as $k | $k, (.[] | [.[$k[]]]) | @csv' data.json
The first line uses Miller's mlr, which converts CSV to JSON and back. The jq line goes the other way: it takes
the header from the first object's keys and writes each row with jq's @csv filter. In Python, csv.DictReader
reads rows as dictionaries, and json.dump writes them out as JSON.