How to Convert CSV to JSON
Converting CSV to JSON gives you an array of objects where each row becomes one object keyed by the header row. The methods below handle the heavy lifting in one line.
pandas (Python)
import pandas as pd
pd.read_csv('data.csv').to_json('data.json', orient='records', indent=2)
orient='records' produces the [{...},{...}] shape most APIs expect.
csvkit (command line)
pip install csvkit
csvjson data.csv > data.json
JavaScript (Node.js)
const fs = require('fs');
const [header, ...lines] = fs.readFileSync('data.csv', 'utf8').trim().split('\n');
const keys = header.split(',');
const json = lines.map(l => Object.fromEntries(l.split(',').map((v, i) => [keys[i], v])));
fs.writeFileSync('data.json', JSON.stringify(json, null, 2));
For production use, swap the naive split for a real parser (papaparse or csv-parse) — it handles quoted commas correctly.
Frequently asked questions
How do I type-cast numbers and booleans?
pandas auto-detects types. With csvkit, all values are strings — post-process in your consumer or use a typed parser.
Can I produce JSON Lines (NDJSON)?
Yes — `pd.read_csv(...).to_json('out.jsonl', orient='records', lines=True)`.