How to Convert CSV to JSON

March 22, 2026 · 6 min read

CSV (Comma-Separated Values) is the universal format for tabular data — spreadsheets, database exports, and data dumps almost always come in CSV. But modern APIs, web apps, and config files expect JSON. Converting between them is one of the most common data tasks developers face.

In this guide, you'll learn how to convert CSV to JSON — using an online converter, in code, and from the command line.

CSV vs. JSON: When to Use Which

CSV is best for flat, tabular data — rows and columns, like a spreadsheet. It's compact and universally supported by Excel, Google Sheets, databases, and data tools.

JSON is best for structured, nested data — objects with properties, arrays, and hierarchical relationships. It's the native format for web APIs and JavaScript applications.

CSV: name,age,city Alice,30,NYC Bob,25,LA JSON: [ { "name": "Alice", "age": "30", "city": "NYC" }, { "name": "Bob", "age": "25", "city": "LA" } ]

The conversion is straightforward: the CSV header row becomes the JSON property names, and each subsequent row becomes an object.

Convert CSV to JSON instantly

Paste CSV data and get formatted JSON output with one click.

Open CSV to JSON Converter

Method 1: Online Converter

The fastest approach — paste your CSV into our CSV to JSON converter and get formatted JSON instantly. No installation, no code, works right in your browser.

Method 2: JavaScript

Here's a simple function to parse CSV to JSON in JavaScript:

function csvToJson(csv) { const lines = csv.trim().split('\n'); const headers = lines[0].split(',').map(h => h.trim()); return lines.slice(1).map(line => { const values = line.split(','); return headers.reduce((obj, header, i) => { obj[header] = values[i]?.trim() || ''; return obj; }, {}); }); } const csv = `name,age,city Alice,30,NYC Bob,25,LA`; console.log(JSON.stringify(csvToJson(csv), null, 2)); // [{ "name": "Alice", "age": "30", "city": "NYC" }, ...]

Caveat: This simple parser doesn't handle quoted fields (values containing commas). For production use, use a library like papaparse:

// Using Papa Parse (npm install papaparse) const Papa = require('papaparse'); const result = Papa.parse(csvString, { header: true }); console.log(result.data); // [{ name: "Alice", age: "30", city: "NYC" }, ...]

Method 3: Python

Python's standard library makes this trivial:

import csv import json # From a CSV file with open('data.csv', 'r') as f: reader = csv.DictReader(f) data = list(reader) # Write to JSON file with open('data.json', 'w') as f: json.dump(data, f, indent=2) print(json.dumps(data, indent=2)) # [{"name": "Alice", "age": "30", "city": "NYC"}, ...]

For a one-liner from the command line:

python3 -c "import csv,json,sys; print(json.dumps(list(csv.DictReader(open(sys.argv[1]))),indent=2))" data.csv

Method 4: Command Line

Using jq (if installed)

# Install: brew install jq (macOS) or apt install jq (Linux) # Convert CSV to JSON using jq and Miller mlr --icsv --ojson cat data.csv

Using csvjson (csvkit)

# Install: pip install csvkit csvjson data.csv

Using Node.js

node -e " const fs = require('fs'); const csv = fs.readFileSync('data.csv', 'utf8'); const lines = csv.trim().split('\n'); const headers = lines[0].split(','); const result = lines.slice(1).map(l => { const vals = l.split(','); return Object.fromEntries(headers.map((h,i) => [h.trim(), vals[i]?.trim()])); }); console.log(JSON.stringify(result, null, 2)); "

Handling Edge Cases

Real-world CSV data is messy. Here are the common pitfalls:

Quoted Fields with Commas

name,description,price "Widget","A small, useful device",9.99 "Gadget","Has ""quotes"" inside",19.99

Fields containing commas or quotes are wrapped in double quotes. Embedded quotes are escaped by doubling them. Simple split(',') won't handle this — use a proper CSV parser.

Type Conversion

CSV is all strings. After converting to JSON, you'll often want to cast types:

// CSV gives you: { "age": "30", "active": "true", "score": "9.5" } // You probably want: { "age": 30, "active": true, "score": 9.5 } function inferTypes(obj) { return Object.fromEntries( Object.entries(obj).map(([k, v]) => { if (v === 'true') return [k, true]; if (v === 'false') return [k, false]; if (v !== '' && !isNaN(v)) return [k, Number(v)]; return [k, v]; }) ); }

Different Delimiters

Not all "CSV" files use commas. TSV (tab-separated), semicolon-separated (common in Europe), and pipe-separated files are all common. Check your delimiter before parsing.

JSON to CSV (The Reverse)

Need to go the other way? Check out our JSON to CSV converter. The key challenge is flattening nested JSON into tabular rows — nested objects need to be serialized or flattened.

Convert Your Data Now

Use the free CSV to JSON converter on UtilShed — paste your data and get clean JSON output instantly.

Related tools for working with data formats: