UUID Studio

CSV to JSON

Turn CSV or TSV rows into a JSON array of objects.

  • 🔒 No data stored or uploaded
  • âš¡ 100% client-side
  • 🆓 Free, no account

Need more than one tool at a time? Open the full Workbench - or press Ctrl+K to jump to any tool.

Create or edit JSON here (syntax colors), then format, validate, or convert - same as MongoDB $binary UUID blobs on the Convert tab once detected.

New document

About CSV to JSON

Converting CSV to JSON looks trivial until you meet real CSV, at which point almost every difficulty comes from the fact that CSV is barely standardised. RFC 4180 describes a common subset, but files in the wild routinely use semicolons or tabs as delimiters, quote inconsistently, and embed newlines inside quoted fields.

Splitting on commas is therefore wrong, and it is the mistake that produces most broken conversions. A field containing a comma must be quoted, a quoted field may contain a newline, and an embedded double quote is escaped by doubling it. Any of those breaks a naive split, usually silently and only for some rows - which is why the failure shows up as a handful of malformed records rather than an obvious error.

Type inference is the other decision. CSV has no types: everything is text. A converter that guesses will turn 007 into 7, 1-2 into a date on some platforms, and a long numeric id into a float that loses its last digits. Keeping everything as strings is the safe default, and casting deliberately per column is the correct one.

Encoding causes the remaining problems. A file exported from Excel on Windows may be in the local codepage rather than UTF-8, or carry a UTF-8 byte-order mark that ends up inside the first column's header name - which is why a header that looks like 'id' fails to match the key 'id' in your code.

Conversion runs in your browser, so a real data export is not uploaded.

How to use the CSV to JSON

  1. Paste the CSV, including the header row.
  2. Check that the detected delimiter is right - semicolons and tabs are common in European and database exports.
  3. Look at the first record's keys for a stray BOM or trailing whitespace in the header names.
  4. Decide whether you want numeric inference or everything as strings; strings are safer for ids and codes.

Examples

  • CSV with header
    id,name
    1,Ada
    2,Grace

CSV to JSON in code

The same operation this tool performs, in the languages you are most likely to need it.

JavaScript - use a real parser
import Papa from "papaparse";

const { data, errors } = Papa.parse(csv, {
  header: true,
  skipEmptyLines: true,
  dynamicTyping: false,   // keep strings - see the pitfalls
  transformHeader: (h) => h.trim().replace(/^\ufeff/, ""),  // strip BOM
});

if (errors.length) console.warn(errors);

// Why not split(","): all three of these break it
// a,"b,c",d          -> comma inside a quoted field
// a,"say ""hi""",c   -> doubled quote as an escape
// a,"line1\nline2",c -> newline inside a quoted field
Python
import csv, json, io

# Sniff the delimiter rather than assuming a comma
sample = raw[:4096]
dialect = csv.Sniffer().sniff(sample, delimiters=",;\t|")

# utf-8-sig strips a BOM if present - otherwise it lands in
# the first header name and key lookups silently fail
reader = csv.DictReader(io.StringIO(raw), dialect=dialect)
rows = [dict(r) for r in reader]
print(json.dumps(rows, indent=2, ensure_ascii=False))

# Cast deliberately, per column, rather than inferring
for r in rows:
    r["quantity"] = int(r["quantity"])
    r["account_id"] = r["account_id"]     # stays a string on purpose
Command line
# Proper CSV parsing, quoted fields and all
csvjson --indent 2 data.csv          # csvkit

# Or with a real parser via python
python3 -c "import csv,json,sys; print(json.dumps(list(csv.DictReader(open(sys.argv[1], encoding='utf-8-sig'))), indent=2))" data.csv

# Detect the encoding and line endings first
file -i data.csv
head -c 3 data.csv | xxd            # ef bb bf = UTF-8 BOM

When you need this

  • Turning a spreadsheet export into JSON for an API request or a test fixture.
  • Loading a CSV report into a script that expects JSON.
  • Checking how a converter handles quoted fields before trusting a bulk import.
  • Diagnosing why some rows of an import came out misaligned.

Common problems and what causes them

Splitting on commas
A quoted field can contain commas, newlines, and doubled double quotes. Splitting on the delimiter breaks on all three, usually for only a few rows, which is why the bug surfaces as scattered bad records rather than an outright failure. Use a real CSV parser.
A byte-order mark inside the first header
A UTF-8 BOM ends up prefixed to the first column name, so a header that displays as 'id' is actually '\ufeffid' and lookups by 'id' return undefined. Read the file as utf-8-sig, or strip the BOM from header names.
Type inference corrupting identifiers
Inference turns 007 into 7, strips leading zeros from postcodes and account numbers, and renders ids above 2^53 imprecisely. Default to strings and cast the columns you actually need as numbers.
The delimiter is not a comma
Semicolons are standard in locales where the comma is the decimal separator, and tabs are common in database exports. Sniff the delimiter rather than assuming.
Inconsistent column counts
Rows with more or fewer fields than the header are common in hand-edited files. Decide explicitly whether to pad, drop or error - silently zipping mismatched rows shifts every value in them.
Empty string versus null
CSV cannot express null, so an empty field is ambiguous. Choose a convention and apply it, especially if the JSON is going to an API that treats missing and null differently.

FAQ

Why can't I just split CSV lines on commas?
Because a quoted field may contain commas, embedded newlines, and doubled quotes as escapes. Splitting handles none of them, and the damage is partial - a few malformed rows rather than an obvious error. Use a parser.
Should numbers be converted automatically?
Usually not. Inference strips leading zeros from ids and postcodes, misreads date-like strings, and loses precision on large integers. Keep everything as strings and cast the specific columns you need.
Why is my first column name wrong?
A UTF-8 byte-order mark at the start of the file becomes part of the first header name. Read with utf-8-sig, or strip \ufeff from headers.
What delimiter should I expect?
A comma per RFC 4180, but semicolons are normal in European locales and tabs in database exports. Detect it from the file rather than assuming - most parsers can sniff it.
What about CSV without a header row?
This tool assumes the first row is the header. If your data has no header, add a placeholder row of column names before pasting it in.

Related reading