Code · Guide

Convert CSV to JSON in Python, JavaScript, and jq

How do I convert CSV to JSON in code?

SHORT ANSWER

In Python use csv.DictReader or pandas.read_csv, in Node use csvtojson or csv-parse, in jq use CSV inputs. All three map header row to keys and handle quoted fields. The same online tool does it locally without code.

Python: csv module

The standard csv module handles RFC 4180 correctly, including quoted fields with delimiters and newlines. DictReader uses the first row as keys by default.

  • import csv, json
  • with open('data.csv') as f: rows = list(csv.DictReader(f))
  • json.dumps(rows, indent=2) for pretty output
  • Use delimiter=';' for semicolon files

Python: pandas

For large or messy tables pandas infers types and handles headers automatically. It is the common choice for data work.

  • import pandas as pd
  • df = pd.read_csv('data.csv')
  • df.to_json(orient='records', indent=2)
  • df.to_json('data.json') to write a file

JavaScript and Node.js

Browsers have no built-in CSV parser, so use a library. csvtojson and PapaParse both handle headers, delimiters, and quoted fields.

  • npm install csvtojson
  • const rows = await csv().fromFile('data.csv')
  • PapaParse alternative: Papa.parse(csvText, {header:true})

jq

For command-line conversion use Miller or jq with CSV input. jq can read CSV and emit JSON with a single filter.

  • jq -R 'split(",")' for simple split (no quotes)
  • Use mlr --icsv --ojson cat data.csv for robust RFC 4180
  • xsv or csvkit also handle large files

Headers, delimiters, and types

The first row usually supplies keys, but some files have no header. Quoted fields that contain commas or newlines must stay in one cell, which DictReader and csvtojson handle. Large files are best streamed, and type inference can be toggled where needed.

Try it without code

If you just need one conversion, paste the CSV into the CSV to JSON tool on this site and press Convert. It handles delimiter auto detection, quoted fields, and large files in a background worker, 100 percent locally. Use code when you need to automate, and the tool when you need one answer quickly.

TRY IT LOCALLY

Try it in your browser with our CSV → JSON. No upload, no server.

Open CSV → JSON →

FAQ

How do I convert CSV to JSON in Python?

import csv; rows = list(csv.DictReader(open('data.csv'))); json.dumps(rows). For pandas: pd.read_csv('data.csv').to_json(orient='records').

How do I convert CSV to JSON in JavaScript?

Use csvtojson: await csv().fromFile('data.csv'), or Papaparse: Papa.parse(csvText, {header:true}).data.

Does CSV to JSON keep numbers as strings?

By default yes, but pandas and type inference can turn numbers and booleans into typed values. The online tool lets you choose.

Can I convert without code?

Yes. Paste the CSV into the CSV to JSON tool and press Convert. It handles headers, delimiters, and large files locally without uploading.