Free online tool

Upload a file

Drop your file hereTap to choose a file

or click to browse, or paste from the clipboard

← Back to Blog

How to Convert CSV to JSON: Shapes, Types, NDJSON and Python

To convert CSV to JSON, use the header row as keys and turn each data row into an object, so sku,price followed by A-100,19.90 becomes [{"sku":"A-100","price":19.90}]. What needs thought is the JSON shape your code expects and which columns become numbers, because CSV stores every value as text. The CSV to JSON Converter shows both before you copy anything, and it runs in your browser, so the file is never uploaded.

How to convert CSV to JSON with Zutilium

  1. Open the CSV to JSON Converter and paste your rows into CSV input, or select Choose file (or drop a file on the box) to open a .csv, .tsv or .txt file of up to 20 MiB.
  2. Under Reading the CSV, Delimiter and Encoding (files only) start on Auto-detect, which handles comma, semicolon, tab and pipe files in UTF-8, UTF-16 or Windows-1252. For numbers written as 19,90, set Decimal separator to Comma (19,90).
  3. Under Output shape, pick Array of objects, Array of arrays, Keyed object, Columns or JSON Lines.
  4. Leave Values on Detect numbers and true/false, or choose Keep everything as text. Then choose what Empty cells become and an Indentation.
  5. In the column list below the two boxes, clear a column's checkbox to leave it out, type a new JSON key, or set its Type to Text, Number or True/false.
  6. Select Copy JSON, or download the result. Pasted text downloads as data.json (data.jsonl for JSON Lines), and a chosen file keeps its own name.

Then check the status line above the output: it names the columns that became numbers and true/false. The column list says why a number-like column stayed text (for example, “02134” has a leading zero), and the preview shows the first 50 parsed rows. To validate the result, use the JSON Formatter & Validator; for TypeScript interfaces, use the JSON to TypeScript Converter.

Pick the JSON shape your code expects

All five shapes below come from this file:

order_id,ship_zip,total,paid
5001,02134,19.90,true
5002,73301,45,false
Shape Use it for
Array of objects APIs, test fixtures, seed files
Array of arrays Compact data read by position
Keyed object Lookups by ID, SKU or language key
Columns Charts and column-by-column code
JSON Lines Large files, BigQuery, jq, logs

Array of objects is the default, with one object per row:

[
  {
    "order_id": 5001,
    "ship_zip": "02134",
    "total": 19.90,
    "paid": true
  },
  {
    "order_id": 5002,
    "ship_zip": "73301",
    "total": 45,
    "paid": false
  }
]

The other four are shown with Indentation set to Minified. Array of arrays begins with the header row unless you clear Start with the header row:

[["order_id","ship_zip","total","paid"],[5001,"02134",19.90,true],[5002,"73301",45,false]]

Keyed object uses the first column as the Key column unless you choose another, and drops that column from each value unless you select Keep the key column inside each object:

{"5001":{"ship_zip":"02134","total":19.90,"paid":true},"5002":{"ship_zip":"73301","total":45,"paid":false}}

Columns writes one array per column, the same layout pandas builds with to_dict(orient="list"):

{"order_id":[5001,5002],"ship_zip":["02134","73301"],"total":[19.90,45],"paid":[true,false]}

JSON Lines writes one minified object per line:

{"order_id":5001,"ship_zip":"02134","total":19.90,"paid":true}
{"order_id":5002,"ship_zip":"73301","total":45,"paid":false}

Keyed objects have two catches. JSON keys are always strings, so 5001 becomes "5001", and JavaScript lists integer-like keys in numeric order rather than file order. A repeated key would also give one object two identical names, which RFC 8259 says makes the result unpredictable.

By default the converter stops and lists those rows, and Repeated keys can instead Group rows into arrays, Keep the first row or Keep the last row. To remove accidental copies first, use the CSV Duplicate Remover. For a flat lookup such as a translation file, pick one column under Value for each key.

CSV to NDJSON (JSON Lines)

NDJSON (newline-delimited JSON, also known as JSON Lines) stores each record as its own line of JSON, with no array around them. The JSON Lines rules are UTF-8 text, a valid JSON value on every line and \n as the line separator, with .jsonl as the suggested extension.

Choose it when the reader works record by record. BigQuery requires newline-delimited JSON when you load JSON files from Cloud Storage, jq reads each line as a separate input, and a log can grow by appending lines. A JSON array, by contrast, is only complete at its closing bracket.

In the converter, select JSON Lines; Indentation is switched off and the download is a .jsonl file. jq converts in either direction:

jq -c '.[]' orders.json > orders.jsonl   # array to JSON Lines
jq -s '.' orders.jsonl > orders.json     # JSON Lines to array

For a CSV over the converter's 20 MiB limit, this standard-library loop reads and writes one row at a time, so the file never has to fit in memory:

import csv
import json

with open("orders.csv", newline="", encoding="utf-8-sig") as src, \
        open("orders.jsonl", "w", encoding="utf-8") as out:
    for row in csv.DictReader(src):
        out.write(json.dumps(row, ensure_ascii=False) + "\n")

Each line comes out as {"order_id": "5001", "ship_zip": "02134", "total": "19.90", "paid": "true"}, with strings only.

How CSV values become numbers, true/false and null

CSV has no data types. RFC 4180, which documents the common CSV format, treats every field as text, so a converter either keeps strings or guesses. Guessing cell by cell can turn one ZIP code into a number while the next stays a string.

Zutilium's converter decides per column. Numbers need every non-empty cell to be a plain decimal such as 45, -3 or 19.90: no leading zero, plus sign or exponent, and 15 significant digits at most. True/false needs every non-empty cell to read true or false, in any letter case. Anything else keeps the column as text, and numbers keep the file's digits, so 19.90 stays 19.90. Three approaches compared on tricky columns:

Values in the column Zutilium (Auto) Python csv pandas defaults
02134 (ZIP code) "02134" "02134" 2134
19.90 and 45 19.90, 45 "19.90", "45" 19.9, 45.0
9007199254740993 (ID) "9007199254740993" "9007199254740993" 9007199254740993
+14155550123 (phone) "+14155550123" "+14155550123" 14155550123
TRUE and false true, false "TRUE", "false" true, false
Empty cell and 10 null, 10 "", "10" null, 10.0

Python's csv module performs no type conversion, as its documentation states, so every value arrives as a string. pandas guesses per column and drops whatever a number can't hold: the leading zero, the plus sign, the trailing zero of 19.90.

When Auto is wrong for your data, change that column's Type. Number also accepts leading zeros, plus signs and exponents (007 becomes 7), True/false also reads yes/no, y/n, t/f, on/off and 1/0, and Text keeps a numeric-looking column as strings. Blank cells follow the column type by default, an empty string in text columns and null in typed ones; the Empty cells menu switches every column to "" or null, or to Leave the key out.

Why long IDs must stay strings

JavaScript reads every JSON number as a 64-bit floating-point value, which represents integers safely only up to 2^53 − 1 (9,007,199,254,740,991), the range RFC 8259 calls interoperable. Past it, digits change silently:

JSON.parse('{"id": 9007199254740993}').id   // 9007199254740992
JSON.parse('{"id": "9007199254740993"}').id // "9007199254740993"

Two different order IDs can collapse into one number. Python's json module reads the same file exactly, because Python integers have no fixed size, so a file can pass a Python test and still break in a browser. The converter caps automatic numbers at 15 significant digits, because every decimal of that length survives a 64-bit float unchanged, so longer IDs, card numbers and tracking codes stay strings.

Convert CSV to nested JSON

CSV is flat, but headers can describe nesting. Select Nest dotted keys (address.city) and numbered keys (tags[0]) into objects and arrays, and address.city becomes a city key inside an address object, while tags[0] and tags[1] become one array:

id,name,address.city,address.zip,tags[0],tags[1]
1,Ana Ruiz,Boston,02134,new,vip
[
  {
    "id": 1,
    "name": "Ana Ruiz",
    "address": {
      "city": "Boston",
      "zip": "02134"
    },
    "tags": [
      "new",
      "vip"
    ]
  }
]
  • Dotted numbers work too: tags.0 and tags.1 form an array when there are two or more, numbered from 0 with none skipped. Any other number is treated as a name, so sales.2024 and sales.2025 produce "sales": {"2024": …, "2025": …}.
  • A header can't be both a value and a parent: with columns named address and address.city, address.city is kept as one literal key.
  • A dotted name typed in a column's JSON key box nests too. Nesting is not available for Array of arrays.

CSV to JSON in Python

The standard library needs no installs. Open the file with newline="", as the csv documentation advises, so line breaks inside quoted values are read correctly, and with utf-8-sig so a byte order mark (BOM) stays out of the first key:

import csv
import json

with open("orders.csv", newline="", encoding="utf-8-sig") as src:
    rows = list(csv.DictReader(src))

with open("orders.json", "w", encoding="utf-8") as out:
    json.dump(rows, out, indent=2, ensure_ascii=False)

Every value in orders.json is a string, "total": "19.90" and "paid": "true" included, and ensure_ascii=False writes é as it is instead of \u00e9. To add types, convert known columns by name before the dump:

for row in rows:
    row["order_id"] = int(row["order_id"])
    row["total"] = float(row["total"])
    row["paid"] = row["paid"].lower() == "true"

That writes the totals as 19.9 and 45.0, because a Python float keeps the value, not the digits in the file.

pandas is shorter but guesses types, so list identifier columns in dtype:

import pandas as pd

df = pd.read_csv("orders.csv", dtype={"ship_zip": str})
df.to_json("orders.json", orient="records", indent=2)
df.to_json("orders.jsonl", orient="records", lines=True)

Without dtype, ship_zip becomes 2134, and to_json rounds floats to 10 decimal places (double_precision) and escapes non-ASCII letters unless you pass force_ascii=False.

On Windows, PowerShell needs no installs either: pipe Import-Csv into ConvertTo-Json. Import-Csv reads every value as a string.

Troubleshooting

ZIP codes, phone numbers or IDs changed

A number type removed what it can't store: the zero in 02134, the plus in +14155550123 or the end of a 16-digit ID. Keep those columns as text (Type set to Text, or dtype=str in pandas). If the CSV passed through a spreadsheet, check it in a text editor, because the zeros may already be gone; CSV to Excel with leading zeros and long order IDs shows how to keep them.

The first key starts with \ufeff or 

The file begins with a UTF-8 byte order mark, and the reader kept it as part of the first header. Python with encoding="utf-8" returns "\ufefforder_id"; use "utf-8-sig" instead. The Zutilium converter removes the mark automatically.

Accents look garbled, or Python raises UnicodeDecodeError

é in place of é means UTF-8 was read as Windows-1252, and a UnicodeDecodeError on byte 0xe9 means the reverse, so open the file with encoding="cp1252". With Encoding (files only) on Auto-detect, the converter reads any file that isn't valid UTF-8 as Windows-1252, so a UTF-8 file with one damaged byte comes out garbled too; export it again as UTF-8.

Everything lands in one key

The file uses semicolons, which Excel writes when the decimal separator is a comma. With Python's default comma delimiter, sku;name;price above A-100;Desk lamp;19,90 becomes {"sku;name;price": "A-100;Desk lamp;19", "null": ["90"]}, so pass delimiter=";" to DictReader. The converter detects semicolons and, when values look like 19,90, suggests setting Decimal separator to Comma (19,90).

Rows have more or fewer values than the header

Often an unquoted comma, as in Chair, mesh, splits a value in two. The converter lists the rows and keeps them, with extra values under new keys such as column4; Rows with a different number of values can instead Keep them, drop extra values or Leave them out. Python's DictReader stores extras in a list under a None key, which json writes as "null", and fills missing fields with null.

Frequently asked questions

Can Excel save a spreadsheet as JSON?

No. Excel's list of supported file formats includes CSV, XML and text formats, but not JSON. Save the sheet as CSV and convert that. If you drop an .xlsx file on the converter, it asks you to export it as CSV first.

What is the difference between JSON and NDJSON?

A JSON document is one value, so a list of records sits inside one pair of square brackets. NDJSON drops the brackets and puts each record on its own line, so programs can stream it, append to it and split it at any line break.

How do I convert JSON back to CSV?

Use the JSON to CSV Converter, which can also write an Excel file. The reverse direction has its own guide: how to convert JSON to CSV or Excel.

Is it safe to convert CSV to JSON online?

Your data never leaves your device with this converter: the conversion runs inside the browser tab, with no upload. Neither the CSV nor the JSON is stored; the page only remembers your options, like the output shape.

How do I convert CSV to JSON in Python?

Read the file with csv.DictReader and write the list with json.dump, as shown above; every value stays a string unless you convert columns yourself. pandas' read_csv plus to_json(orient="records") is shorter but guesses types, so pass dtype for ZIP codes and IDs.