CSV (Comma-Separated Values) is the most portable data format in existence. Nearly every tool that handles tabular data can export CSV. But when you need to send that data to an API, import it into a document database, or use it in a JavaScript application, you need JSON.
This guide covers four practical methods to convert CSV to JSON, from the simplest online tools to scripting approaches for automation.
What Changes When You Convert CSV to JSON
Understanding the conversion helps you anticipate the output:
- Each row in the CSV becomes a JSON object
- The header row becomes the keys of each object
- The entire file becomes a JSON array of those objects
- All values are strings by default โ type inference is optional
Given this CSV:
name,age,city
Alice,30,London
Bob,25,ParisThe resulting JSON would be:
[
{ "name": "Alice", "age": "30", "city": "London" },
{ "name": "Bob", "age": "25", "city": "Paris" }
]"age" to be the number 30 rather than the string "30", you will need a converter with type inference or a post-processing step. Method 1: Online CSV to JSON Converters
For quick, one-off conversions, online tools are the fastest option. Search for "CSV to JSON converter" โ the top results are all reliable. Most offer:
- Paste CSV text or upload a
.csvfile - Automatic header detection
- Optional type inference (converts numbers and booleans)
- Copy or download the resulting JSON
When to use online tools
- Single files, not a recurring task
- Non-sensitive data (avoid uploading confidential CSVs to public websites)
- Small to medium files (under 5MB)
Method 2: Python with the csv Module
Python's standard library includes everything you need โ no packages to install:
import csv
import json
with open('data.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
rows = list(reader)
with open('output.json', 'w', encoding='utf-8') as jsonfile:
json.dump(rows, jsonfile, indent=2)
print(f"Converted {len(rows)} rows to output.json")csv.DictReader automatically uses the first row as keys, giving you a list of dictionaries that maps directly to a JSON array of objects.
Adding Type Inference
To convert numbers and booleans automatically:
import csv
import json
def infer_type(value):
if value.lower() == 'true': return True
if value.lower() == 'false': return False
try: return int(value)
except ValueError: pass
try: return float(value)
except ValueError: pass
return value
with open('data.csv', 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
rows = [{k: infer_type(v) for k, v in row.items()} for row in reader]
with open('output.json', 'w', encoding='utf-8') as f:
json.dump(rows, f, indent=2)Method 3: Python with pandas
If you already use pandas for data work, the conversion is a single line:
import pandas as pd
df = pd.read_csv('data.csv')
df.to_json('output.json', orient='records', indent=2) The orient='records' parameter produces the standard array-of-objects format. pandas also handles type inference automatically โ numbers are read as numbers, not strings.
Controlling the Output Format
pandas supports several JSON output orientations:
| orient | Output Shape | Use Case |
|---|---|---|
records | Array of objects | Standard API format |
index | Object keyed by row index | When row identity matters |
split | Separate columns, index, data arrays | Compact format for large datasets |
table | Includes schema metadata | Self-describing datasets |
Method 4: Node.js with the csv-parse Library
For JavaScript developers or projects already using Node.js:
npm install csv-parseconst { parse } = require('csv-parse/sync');
const fs = require('fs');
const csvContent = fs.readFileSync('data.csv', 'utf8');
const records = parse(csvContent, {
columns: true, // use first row as keys
skip_empty_lines: true,
cast: true // auto type inference
});
fs.writeFileSync('output.json', JSON.stringify(records, null, 2));
console.log(`Converted ${records.length} rows`); The cast: true option enables automatic type inference โ numbers and booleans are correctly typed in the output.
Handling Common CSV Quirks
Values with commas
CSV values that contain commas must be wrapped in double quotes in the source file. All the libraries above handle this correctly when they see "London, UK" as a quoted field.
Different delimiters
Some "CSV" files use semicolons, tabs, or pipes as delimiters. Specify the delimiter explicitly:
# Python โ semicolon delimiter
reader = csv.DictReader(csvfile, delimiter=';')
# pandas โ tab-separated
df = pd.read_csv('data.tsv', sep='\t')Encoding issues
Always open files with encoding='utf-8' in Python. Files exported from older Excel versions on Windows may use encoding='cp1252' or 'latin-1'.
Missing values
Empty CSV cells become empty strings "" by default. You may want to convert them to null in your JSON for a cleaner output.
Need to Go the Other Way?
Convert JSON back to a spreadsheet with our free JSON to Excel converter โ handles nested structures automatically.
Convert JSON to Excel Now โ