How to Convert CSV to JSON (Online, Python, and JavaScript)
CSV and JSON are the two formats data moves in most often: CSV comes out of spreadsheets and database exports, and JSON goes into APIs, config, and code. Converting between them sounds trivial, and for clean data it is, but quoted fields, data types, and ragged rows turn a “quick” conversion messy fast. This guide covers how the mapping works, the quickest way to convert CSV to JSON online, and how to do it in Python and JavaScript, along with the gotchas that trip people up.
The fastest route: a browser converter
For a one-off conversion, an online tool beats writing code. Paste your CSV into the CSV to JSON converter and it produces a JSON array of objects, using your header row as the keys. It detects the delimiter automatically, handles quoted fields, and can optionally convert numbers and booleans to real JSON types. Because it runs entirely in your browser, the data never leaves your machine, which matters when the CSV is an export of real records.
Once you have JSON, the JSON formatter will pretty-print or validate it, and the JSON viewer renders it as a collapsible tree so you can sanity-check the structure before using it.
How CSV maps to JSON
Every converter makes the same handful of decisions. Knowing them tells you what to expect from the output.
- The header row becomes the keys. The first row’s column names become the keys of each object, so each data row turns into one object.
- The file becomes an array. The whole CSV becomes a JSON array, with one object (or one array) per data row.
- Quoted fields are preserved. A field wrapped in double quotes can contain commas, line breaks, and escaped quotes (written as
""), and stays a single value. - Types are optional. A converter can leave every value as a string, or coerce
42to a number andtrueto a boolean. Which you want depends on the consumer.
Here is the shape of a typical conversion:
name,age,active
Ada,36,true
Alan,41,false
becomes:
[
{ "name": "Ada", "age": 36, "active": true },
{ "name": "Alan", "age": 41, "active": false }
]
Notice each row became an object keyed by the header, and the numeric and boolean fields became real JSON types rather than strings.
Convert CSV to JSON in Python
Python’s standard library has everything you need in the csv module. DictReader reads each row into a dictionary keyed by the header, which maps almost directly onto JSON objects:
import csv
import json
with open("data.csv", newline="", encoding="utf-8") as f:
rows = list(csv.DictReader(f))
print(json.dumps(rows, indent=2))
That handles quoted fields and embedded commas correctly, because csv is a real parser rather than a split(","). The one thing it does not do is type conversion: every value comes back as a string. If you want real numbers and booleans, coerce them explicitly:
def typed(value):
if value in ("true", "false"):
return value == "true"
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return value
rows = [{k: typed(v) for k, v in row.items()} for row in rows]
Be deliberate about typing: converting 007 to 7 loses the leading zero, which matters for ZIP codes and IDs, so many pipelines leave such columns as strings on purpose.
Convert CSV to JSON in JavaScript
In Node or the browser, the robust choice is a real CSV parser rather than split. The popular papaparse library handles quoting, delimiters, and typing in one call:
import Papa from "papaparse";
const result = Papa.parse(csvText, {
header: true,
dynamicTyping: true,
skipEmptyLines: true,
});
console.log(JSON.stringify(result.data, null, 2));
header: true turns each row into an object, and dynamicTyping: true converts numbers and booleans. For a dependency-free version that covers simple, well-formed CSV, you can split lines and map them, but be aware this breaks the moment a field contains a quoted comma or a line break:
const [head, ...lines] = csvText.trim().split("\n");
const keys = head.split(",");
const data = lines.map(line => {
const cells = line.split(",");
return Object.fromEntries(keys.map((k, i) => [k, cells[i]]));
});
Use the naive version only for data you control; reach for a real parser for anything from users or exports.
The gotchas that bite
A few things go wrong often enough to plan for:
- Commas inside fields. The classic
split(",")mistake. A value like"Maida Vale, London"must stay one field, which only a quote-aware parser gets right. - Over-eager type conversion. Turning
007into7, or a long numeric ID into a float that loses precision, corrupts data silently. Keep identifier columns as strings. - Ragged rows. Rows with more or fewer columns than the header need a rule. A good converter fills missing values with
nulland keeps extras under an indexed key rather than dropping data. - Delimiters that are not commas. European exports often use semicolons, and some files use tabs or pipes. Detect or set the delimiter explicitly.
- Encoding and BOM. A UTF-8 byte-order mark on the first header can corrupt the first key name. Read files as UTF-8 and strip the BOM if present.
Going the other way
Need to turn JSON back into a spreadsheet? That is the reverse trip, and the JSON to CSV tool handles it. If you are moving data between other formats too, the JSON to XML converter and the YAML formatter cover those. And if the JSON you produce will not parse, the JSON parser points to the exact error.
Frequently asked questions
How do I convert CSV to JSON without writing code?
Paste the CSV into the CSV to JSON converter, confirm the delimiter and header options, and copy or download the JSON. It runs in your browser, so nothing is uploaded.
Does the first row become the JSON keys?
Yes, when you treat the first row as a header. Each subsequent row becomes an object whose keys are the column names. Turn the header option off to get an array of arrays instead.
How are commas inside a value handled?
A field wrapped in double quotes can contain commas and line breaks and still counts as one value. This is why a real CSV parser beats splitting on commas, which would break such fields apart.
Should I convert numbers automatically?
Usually yes, but keep identifier-like columns as strings. Converting 007 to 7 or a huge ID to a float loses information, so type numeric measures and leave codes, ZIPs, and IDs as strings.
For the rest of the toolkit, including formatting, validating, and converting JSON, see the roundup of free JSON tools, or learn the format itself in what JSON is.
Leave a Reply