CSV to JSON Format: Convert with Python, Node and Excel
November 11, 2026 · 11 min read
CSV to JSON Format: Turn Rows into Arrays and Objects
Converting CSV to JSON format turns the header row into keys and each data row into one object, so the whole file becomes a JSON array. You also decide the output shape and which cells become numbers, booleans or null. Below you'll find working Python, Node, PowerShell and Excel routes for each choice.
What Does the CSV to JSON Format Look Like?
CSV has no data types. RFC 4180 describes it as lines of comma-separated fields with an optional header line, and every field is plain text. JSON, defined in RFC 8259, has strings, numbers, true, false, null, objects and arrays. Most of the work is deciding how that text maps onto those types.
Here's the sample file used throughout this post. It has a zip code with a leading zero, a name containing a comma and two empty cells.
id,name,zip,active,joined
1,Ana Ruiz,02134,true,2026-01-15
2,"Lee, Sam",94105,false,2026-02-03
3,Priya Nair,,true,
A plain conversion with no type handling gives you an array of objects:
[
{ "id": "1", "name": "Ana Ruiz", "zip": "02134", "active": "true", "joined": "2026-01-15" },
{ "id": "2", "name": "Lee, Sam", "zip": "94105", "active": "false", "joined": "2026-02-03" },
{ "id": "3", "name": "Priya Nair", "zip": "", "active": "true", "joined": "" }
]
The header names became keys, each row became one object in file order, and the file became the outer array. The quotes around Lee, Sam are gone because they were CSV syntax, not data. Every value is still a string, which is the safe starting point.
Array of Objects, Keyed Object or JSON Lines: Choosing the Output Shape
The same rows fit four common shapes. You've already seen the array of objects, so here are the other three, trimmed to three columns for readability.
{
"1": { "name": "Ana Ruiz", "zip": "02134" },
"2": { "name": "Lee, Sam", "zip": "94105" },
"3": { "name": "Priya Nair", "zip": "" }
}
This first one is a keyed object, with rows indexed by the id column. JSON object keys are always strings, so the id 1 becomes "1" even after you type the other columns.
[
["id", "name", "zip"],
["1", "Ana Ruiz", "02134"],
["2", "Lee, Sam", "94105"],
["3", "Priya Nair", ""]
]
Next is an array of arrays, the raw grid. The header is just the first inner array, or you drop it.
{"id": "1", "name": "Ana Ruiz", "zip": "02134"}
{"id": "2", "name": "Lee, Sam", "zip": "94105"}
{"id": "3", "name": "Priya Nair", "zip": ""}
Last comes JSON Lines: one complete JSON value per line, no outer brackets, and a .jsonl extension by convention.
How to pick:
- Array of objects suits REST APIs, front-end code and most import screens.
- Keyed object suits lookups by id, such as a config map or a cache.
- Array of arrays suits charting libraries and very wide files where repeated keys would bloat the output.
- JSON Lines suits streaming, logs and bulk loaders that read one record at a time.
We prefer the array of objects unless you need lookups by key, because every language reads it without extra code. Headers with dots, such as address.city, stay literal keys in all four shapes. Some converters expand them into nested objects, but that's a choice you make in code, not something CSV encodes. Going the other way, our JSON to CSV guide shows how nested objects flatten into dotted columns.
How Do You Convert CSV to JSON in Python?
For a conversion with nothing to install, use Python's standard library. csv.DictReader uses the first row as keys, and json.dump writes the file.
import csv, json
with open("customers.csv", newline="", encoding="utf-8-sig") as f:
rows = list(csv.DictReader(f))
with open("customers.json", "w", encoding="utf-8") as f:
json.dump(rows, f, indent=2)
This writes the all-strings array from the first section, with each key on its own line. The indent=2 argument pretty-prints the output, so you don't need a separate CSV to JSON formatter. Open the file with newline="", as the Python csv docs advise, so quoted line breaks survive. The utf-8-sig encoding strips a byte order mark. Without it, a file that starts with one gives you a first key of \ufeffid instead of id.
For typed output, most CSV to JSON Python scripts reach for pandas:
import pandas as pd
df = pd.read_csv("customers.csv", dtype={"zip": str})
df.to_json("customers.json", orient="records", indent=2)
That gives you numbers and booleans (whitespace tidied here):
[
{ "id": 1, "name": "Ana Ruiz", "zip": "02134", "active": true, "joined": "2026-01-15" },
{ "id": 2, "name": "Lee, Sam", "zip": "94105", "active": false, "joined": "2026-02-03" },
{ "id": 3, "name": "Priya Nair", "zip": null, "active": true, "joined": null }
]
The orient argument picks the shape, per the pandas to_json reference. Use orient="records" for an array of objects, and add lines=True for JSON Lines. Call df.set_index("id") before orient="index" for the keyed object, and use orient="values" for an array of arrays without the header row. We tested these examples on pandas 3.1.1.
Converting a CSV File in Node.js and PowerShell
Node's standard library doesn't include a CSV parser, and splitting lines on commas breaks the moment a field contains one ("Lee, Sam" would become two columns). Install PapaParse with npm install papaparse and let it do the parsing:
import fs from "node:fs";
import Papa from "papaparse";
const text = fs.readFileSync("customers.csv", "utf8");
const result = Papa.parse(text, {
header: true,
skipEmptyLines: true,
dynamicTyping: { id: true, active: true },
});
if (result.errors.length) console.error(result.errors);
fs.writeFileSync("customers.json", JSON.stringify(result.data, null, 2));
With header: true, each row becomes an object. The per-column dynamicTyping object turns id into numbers and active into booleans, while zip keeps its leading zero. Without skipEmptyLines, the trailing newline at the end of the file can produce an extra, nearly empty object. The PapaParse docs list every config option.
On Windows, PowerShell 7 does it in one line:
Import-Csv .\customers.csv | ConvertTo-Json -AsArray | Set-Content .\customers.json
Import-Csv reads every value as a string, so the result matches the all-strings output. The -AsArray switch keeps the outer brackets even when the file has a single row. Windows PowerShell 5.1 doesn't have that switch.
Numbers, Booleans and Empty Cells: Type Rules That Change Your JSON
The same cell can come out three different ways depending on the tool. Here's what our sample produced, with pandas on its defaults and PapaParse set to dynamicTyping: true.
| CSV cell | Python csv module | pandas read_csv | PapaParse dynamicTyping |
|---|---|---|---|
1 | "1" | 1 | 1 |
02134 | "02134" | 2134.0 | 2134 |
true | "true" | true | true |
| empty | "" | null | null |
2026-01-15 | "2026-01-15" | "2026-01-15" | "2026-01-15" |
The zip column shows the biggest trap. pandas reads it as a number, and because one cell is empty the whole column becomes a float, so 02134 turns into 2134.0. Fix it with dtype={"zip": str} in pandas or by leaving the column out of PapaParse's typing. Treat zip codes, phone numbers and account ids as strings, because nobody does arithmetic on them. Config files hit the same trap, and our YAML to JSON guide lists the values that change type.
Booleans matter too. If you keep strings, the value "false" is truthy in JavaScript, so if (row.active) passes for every row. Either type the column or compare against the string.
Empty cells need one rule applied everywhere: an empty string, null, or a missing key. Pick based on what the consumer expects. With pandas, keep_default_na=False keeps empty cells as empty strings.
Dates stay strings unless you parse them. If you pass parse_dates to pandas, also pass date_format="iso" to to_json. Otherwise pandas 3.1 writes millisecond timestamps and warns that the epoch format is deprecated.
Quoted Commas, Line Breaks and Semicolons in the Source CSV
RFC 4180 says a field containing a comma, a double quote or a line break should be wrapped in double quotes. A quote inside a quoted field must be doubled. Real parsers handle this; string splitting doesn't.
id,note
4,"Said ""hi"", then left
early"
Python's csv module, pandas and PapaParse all read that as one row. The csv module produces:
[{ "id": "4", "note": "Said \"hi\", then left\nearly" }]
The doubled quotes become escaped quotes and the line break becomes \n inside the JSON string. pandas gives the same note but types the id as the number 4. Spreadsheets set to European locales often export semicolon-separated files. Set the delimiter explicitly: sep=";" in pandas, delimiter=";" in the csv module, delimiter: ";" in PapaParse and -Delimiter ';' for Import-Csv. If those files also use decimal commas, add decimal="," to read_csv.
How Can You Convert a CSV File to JSON Online or From Excel?
Excel's Save As list has no JSON option. To export CSV to JSON from a workbook, save the sheet as CSV first. Choose File, then Save As, then CSV (Comma delimited), or a UTF-8 CSV option if your version lists one. Excel saves only the active sheet, so repeat it per sheet. Then run any of the routes above on the file.
If you'd rather not run code, you can convert CSV to JSON format in two steps with our browser tools. Paste the CSV into the CSV to Markdown tool, which auto-detects comma, tab, semicolon or pipe separators and builds a table. Then paste that table into the JSON to Markdown tool, which also converts a Markdown table back to JSON. Both run in your browser with no account.
Our CSV to Markdown table guide covers the first step. Our JSON to Markdown guide explains the opposite trip, from JSON records to a table.
Here's our sample as a Markdown table, trimmed to three columns. That's the format the first step produces. Edit a cell and the preview updates:
The limitation: this isn't a one-click online CSV to JSON converter. The table step keeps cell values only, so check how numbers, booleans and empty cells come out. When types matter, use the Python or Node code instead.
Checking the Converted JSON Before You Ship It
A conversion can finish without errors and still be wrong. Four quick checks catch most problems.
- Compare counts. The array length should equal the number of data rows. Count with the parser, not
wc -l: our quoted-line-break example is 3 lines but 1 row. - Validate the file. Run
python -m json.tool customers.json, which prints the error position for invalid JSON. Add--json-linesfor JSON Lines. In Node,JSON.parsethrows instead. - Spot-check three rows: the first, the last and any row with quotes, commas or empty cells. If the file is minified, the VS Code formatter expands it to one property per line first.
- Fix headers first. Duplicate or blank headers fail quietly. Given
id,name,name, Python'sDictReaderkeeps only the secondnamevalue. pandas renames it toname.1, and PapaParse toname_1.
Questions About Turning Spreadsheet Rows into JSON
The right CSV to JSON format depends on who reads the file. Use an array of objects for APIs, a keyed object for lookups and JSON Lines for streams, then set the type rules on purpose. Start with the Python or Node snippets above, and check the counts before you ship. To eyeball the rows as a table first, paste the converted Markdown table into the editor.