How to Convert CSV to JSON: Types, Empty Cells and Nested Keys
By the DataSwitch Pro team · · 4 min read
Quick answer
To convert CSV to JSON, read the first line as column names and turn each following line into an object with those names as keys, producing an array of objects. Use a proper CSV parser — not a simple split on commas — so that quoted values with commas or line breaks stay intact. You can paste the file into a CSV to JSON converter, use Python's csv.DictReader with json.dump, or a library such as Papa Parse in JavaScript.
CSV is the most common export format for spreadsheets, CRMs and databases; JSON is what web applications and APIs consume. Converting between them looks trivial until you meet a comma inside a quoted address, a semicolon-separated export, or a column of ZIP codes that loses its leading zeros.
The basic mapping
orders.csv
id,customer,total,paid
1001,"Benali, Sara",120.50,true
1002,Tom Martin,18,false
orders.json
[
{ "id": 1001, "customer": "Benali, Sara", "total": 120.5, "paid": true },
{ "id": 1002, "customer": "Tom Martin", "total": 18, "paid": false }
]
Notice the first record: the customer name contains a comma, so the CSV wraps it in quotes. Splitting each line on commas would break it into two fields. Any serious conversion must follow the CSV quoting rules described in RFC 4180: quoted fields may contain commas, line breaks and doubled quotes ("").
Method 1: Online converter
- Paste the CSV or drop the file into the CSV to JSON converter.
- Check the delimiter: comma, semicolon, tab or pipe are detected automatically, and you can force one under Options.
- Choose whether numbers and true/false should be typed or kept as strings.
- Copy or download the JSON.
Method 2: Python (standard library only)
Python
import csv, json
with open("orders.csv", newline="", encoding="utf-8-sig") as f:
rows = list(csv.DictReader(f))
with open("orders.json", "w", encoding="utf-8") as f:
json.dump(rows, f, ensure_ascii=False, indent=2)
DictReader handles quoting correctly. utf-8-sig removes the byte-order mark Excel adds to UTF-8 files, which would otherwise end up glued to the first column name. Every value comes out as a string; convert types yourself if you need numbers, or use pandas (pd.read_csv(...).to_json(orient="records")), which infers types — while keeping dtype=str for ID columns.
Method 3: JavaScript
In the browser or Node.js, the Papa Parse library is the usual choice:
JavaScript
const result = Papa.parse(csvText, { header: true, dynamicTyping: true, skipEmptyLines: true });
const json = JSON.stringify(result.data, null, 2);
dynamicTyping turns numeric and boolean text into real numbers and booleans. Turn it off for files with codes that must stay text.
Decisions that change the result
- Types. CSV has no types; everything is text. Converting
"120.50"to120.5is convenient, but values like00412or1e5can be damaged. When in doubt, keep strings and convert specific fields in code. - Empty cells. An empty field can become
""ornull. Databases and APIs often treat these differently, so pick what the receiver expects. - Duplicate or empty headers. Two columns named
emailoverwrite each other as object keys. Rename them before converting. - Nested keys. If headers use dots, such as
address.city, some tools rebuild nested objects from them; others keep the dotted names as plain keys. Check which one your code expects. - Delimiter. Exports from European versions of Excel often use semicolons. If the JSON has a single key containing the whole header line, the delimiter was wrong.
Arrays, JSON Lines or an object keyed by ID
An array of objects is the default, but not the only shape. Log processors and big-data tools prefer JSON Lines, one object per line — convert the array with JSON to JSONL. Lookup tables are sometimes easier as an object keyed by ID, such as {"1001": {...}}, which takes one extra line of code after converting.
Large files
A few hundred megabytes of CSV becomes noticeably larger as JSON, because every key is repeated in every record. For very large files, stream the conversion row by row (Python's csv module does this naturally) and write JSON Lines instead of one giant array, so that the reader does not need to load everything into memory.
Try it on your own file. Free, private, and processed in your browser.
Frequently asked questions
Why is my whole header one JSON key?
The delimiter was not detected. The file probably uses semicolons or tabs; choose the right delimiter and convert again.
How do I keep ZIP codes and IDs as text?
Turn off number detection, or keep those specific columns as strings. Numbers cannot store leading zeros.
Can I convert JSON back to CSV?
Yes, as long as the JSON is a list of records. Nested objects become dot-named columns; see JSON to CSV.
Does the CSV need a header row?
For an array of objects, yes, because the header provides the keys. Without one, add a header line first or convert each row to an array of values.