TheFileTools

How to convert a CSV file to JSON

Turn a spreadsheet export into a JSON list of objects, and know how headers, numbers, quotes and separators are handled.

Spreadsheets export CSV, while scripts, APIs and many import tools want JSON.

Steps

  1. Open CSV to JSON.
  2. Paste the table, or press Open a file and choose a .csv, .tsv or .txt file. The file is read on your device and is not uploaded.
  3. Check the result, then press Copy, or Download to save it as data.json.

How the table is read

  • The first row is the header. Each later row becomes one object with the header names as keys. An empty header cell is named column1, column2 and so on by its position. If two columns share a name, only the later one survives.
  • The separator is detected. Comma, semicolon, tab or the pipe character: whichever appears most often in the first line is used. There is no setting for it.
  • Quoted cells are respected. A cell in double quotes may contain the separator and line breaks, and two double quotes in a row stand for one.
  • Empty lines are skipped, and an empty cell becomes an empty string, not null.

Which values become numbers

Plain numbers such as 34 or -2.5 become JSON numbers, and the exact words true and false become booleans. Everything else stays a string. Take a file with the header name;age;zip and the row Anna;34;00123. The result is one object with "name": "Anna", "age": 34 and "zip": "00123".

  • A value with a leading zero stays a string, so postcodes and phone numbers are not damaged.
  • Numbers of 16 characters or more stay strings, so long identifiers are not rounded.
  • A decimal comma, as in 3,5, is not recognised as a number. It stays a string, and in a comma-separated file it must be quoted or it splits the cell.
  • Quoting a number does not keep it a string: "34" in the file still becomes 34.

After converting

The output is indented with two spaces. To minify it or change the indent, paste it into JSON formatter. For the opposite direction use JSON to CSV.