mygems

Convert a CSV to JSON

Every row an object, every header a key.

  • Array, JSON Lines or arrays
  • Numbers and booleans detected
  • IDs stay strings
  • Nothing uploaded

1.Open the file

CSV or TSV. Drag it here, or choose a file.

Why it exists

CSV to JSON looks like the easy direction, and the usual converters make it easy by getting the types wrong: everything becomes a string, or everything that looks numeric becomes a number, including the ZIP code with the leading zero and the 18-digit ID that JavaScript cannot represent exactly. This decides each column from all of its values and leaves the ones that only look like numbers alone.

What it does

  • Writes an array of objects keyed by the header row, JSON Lines with one object per line, or an array of arrays with the header first.
  • Detects columns that are entirely numbers or true/false and writes them unquoted.
  • Keeps columns with leading zeros or more than 15 digits as strings, because a JSON number would change them.
  • Turns empty cells into null rather than an empty string, or keeps everything as strings if you prefer.
  • Indents for reading, or writes the smallest file possible.

Which shape to pick

An array of objects is what most APIs and JavaScript code expect. JSON Lines suits large files and data tools that stream one record at a time, such as BigQuery, Elasticsearch and jq. An array of arrays is the most compact, and keeps the column order explicit.

Questions

Why is my ID column still a string?
Because turning it into a number would change it. A leading zero is dropped by any JSON parser, and integers past 15 or 16 digits lose precision in JavaScript. If every value in a column is a plain number without either problem, it is written as a number.
What happens to empty cells?
They become null when type detection is on, since a CSV has no way to tell an empty string from a missing value. With detection off, every value, empty ones included, is a string.
Can it handle nested JSON?
CSV is flat, so the JSON it produces is flat: one level of keys per record. Going the other way, from nested JSON to CSV, is what the JSON to CSV tool does.