CSV stands for “comma-separated values”. It is a plain text table. Each line is a row, and commas split the row into cells. Spreadsheets and databases can all export it.
Many apps and APIs want JSON instead. This guide shows how CSV turns into JSON. You will learn to convert it online, in JavaScript and in Python. It also covers the small details that often go wrong.
Quick answer: Use the first row of the CSV as keys. Turn each of the other rows into a JSON object. The result is an array (a list) of objects. A free online CSV to JSON converter can do this for you. So can a few lines of code.
How does CSV map to JSON?
Here is a small CSV file. The first line is the header row, which holds the column names:
name,city,zip
Ada,London,02134
Linus,Helsinki,00100Converted to JSON, each column name becomes a key, and each row becomes an object:
[
{ "name": "Ada", "city": "London", "zip": "02134" },
{ "name": "Linus", "city": "Helsinki", "zip": "00100" }
]If your CSV has no header row, there are no names to use as keys. You then get an array of arrays instead, with one inner list per row.
How to convert CSV to JSON online, step by step
- Open the CSV to JSON converter.
- Paste your CSV into the left editor, or upload a
.csvor.tsvfile. - Keep “First row is header” on to get an array of objects. Turn it off to get an array of arrays.
- Copy the JSON, or download it as a
.jsonfile.
The converter finds the delimiter for you and handles quoted fields, which are explained below.
Delimiters: commas, semicolons and tabs
The delimiter is the character that separates one cell from the next. The name says “comma”, but CSV files don’t always use commas:
- Comma is the most common choice.
- Semicolon is common in countries where the comma is used for decimals, like
3,5. Excel in those regions often saves CSV files with semicolons. - Tab is used in TSV (tab-separated values) files.
- Pipe (
|) shows up in some database exports.
The CSV to JSON converter looks for commas, semicolons, tabs and pipes by itself. If it guesses wrong, you can pick the delimiter yourself.
Quoted fields
What if a cell has a comma in it? Then the cell is put inside double quotes. A double quote inside a quoted cell is written twice:
name,comment
Ada,"Likes math, music and tea"
Bob,"He said ""hi"""This becomes:
[
{ "name": "Ada", "comment": "Likes math, music and tea" },
{ "name": "Bob", "comment": "He said \"hi\"" }
]Quoted cells can even have line breaks inside them. So just splitting each line at the commas is not safe for real files. A real CSV parser (a tool that reads CSV) handles all of these cases.
Numbers vs text: watch your ZIP codes
CSV has no data types. Every cell is just text. But JSON has numbers, strings (text), booleans (true or false) and null. So a converter has to decide which type each value should be.
Turning 42 into a number is usually right. But turning the ZIP code 02134 into the number 2134 loses data. The same goes for phone numbers and some IDs.
In the online converter, “Detect numbers & booleans” turns 42 into a number and true into a boolean. Values that start with a zero, like 02134, stay as text. Very long IDs are also safer as text. Read JSON and big numbers to see why. For a short look at all the types, see JSON data types.
Convert CSV to JSON in JavaScript
For simple data with no quoted cells, a few lines of JavaScript are enough:
const csv = `name,city,zip
Ada,London,02134
Linus,Helsinki,00100`;
const [headerLine, ...lines] = csv.trim().split(/\r?\n/);
const headers = headerLine.split(',');
const rows = lines.map((line) => {
const values = line.split(',');
const row = {};
headers.forEach((key, i) => {
row[key] = values[i];
});
return row;
});
console.log(JSON.stringify(rows, null, 2));This code breaks on quoted cells that have commas or line breaks. For real files, use a CSV library such as Papa Parse. It handles those cases for you.
Convert CSV to JSON in Python
Python comes with a csv module and a json module, so you don’t need to install anything:
import csv
import json
with open('people.csv', newline='', encoding='utf-8') as f:
rows = list(csv.DictReader(f))
with open('people.json', 'w', encoding='utf-8') as f:
json.dump(rows, f, indent=2, ensure_ascii=False)A few tips:
csv.DictReaderuses the first row as keys and handles quoted fields correctly.- For a semicolon file, use
csv.DictReader(f, delimiter=';'). For tabs, usedelimiter='\t'. - Every value comes out as a string. Convert numbers yourself, for example
row['age'] = int(row['age']). - If the first key looks odd, the file may start with a hidden marker that Excel adds. Open it with
encoding='utf-8-sig'instead.
Starting from an Excel file?
You don’t have to save it as CSV first. The Excel to JSON converter reads .xlsx files directly in your browser. The first row becomes the keys, numbers stay numbers, and dates become standard date strings.
Common questions
Can I convert CSV to JSON without a header row?
Yes. Turn off “First row is header” in the converter. You get an array of arrays, with one inner array per row.
Why did my ZIP codes lose their leading zeros?
They were turned into numbers, and numbers can’t start with zero. Keep those columns as text. Or use a converter that does not change values that start with zero.
How do I convert JSON back to CSV?
Use the JSON to CSV converter. Nested objects become columns like address.city. For the details, read how to convert JSON to CSV without losing data.
What is the difference between CSV and TSV?
Only the delimiter. CSV usually separates cells with commas, and TSV uses tabs. Both convert to JSON the same way.