CSV to JSON Conversion
Converting CSV to JSON is a common task when you need to prepare data for web applications, APIs, or modern databases. Learn how to do it effectively.
Understanding the Conversion
CSV (Comma-Separated Values) and JSON (JavaScript Object Notation) serve different purposes:
- CSV is a simple, flat tabular format - great for spreadsheets
- JSON supports complex nested structures and multiple data types
Converting CSV to JSON creates structured data that's perfect for web applications and APIs.
Conversion Process
Simple Example
Given this CSV data:
name,age,city
John,30,New York
Jane,25,Boston
Bob,35,Chicago
It converts to this JSON:
[
{
"name": "John",
"age": 30,
"city": "New York"
},
{
"name": "Jane",
"age": 25,
"city": "Boston"
},
{
"name": "Bob",
"age": 35,
"city": "Chicago"
}
]
Conversion Tools
Our Online Tool
Try our free CSV to JSON Converter with features like:
- Multiple delimiter support (comma, semicolon, tab, pipe)
- Automatic header detection
- Type inference for numbers
- Instant download
Command Line Tools
For batch processing, you can use command-line tools:
# Using csvtojson (Node.js)
npm install -g csvtojson
csvtojson input.csv > output.json
# Using Python pandas
import pandas as pd
df = pd.read_csv('input.csv')
df.to_json('output.json', orient='records')
Programmatic Conversion
Using JavaScript
function csvToJSON(csv) {
const lines = csv.split('\\n');
const headers = lines[0].split(',');
return lines.slice(1).map(line => {
const values = line.split(',');
return headers.reduce((obj, header, index) => {
obj[header.trim()] = values[index]?.trim();
return obj;
}, {});
});
}
// Usage
const csvData = `name,age,city
John,30,New York
Jane,25,Boston`;
const jsonData = csvToJSON(csvData);
console.log(JSON.stringify(jsonData, null, 2));
Using Python
import csv
import json
def csv_to_json(csv_file, json_file):
data = []
with open(csv_file, 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
data.append(row)
with open(json_file, 'w') as file:
json.dump(data, file, indent=2)
# Usage
csv_to_json('input.csv', 'output.json')
Handling Complex Cases
1. Different Delimiters
Not all CSVs use commas. Handle different delimiters:
- Semicolon (;) - common in European locales
- Tab (\\t) - tab-separated values (TSV)
- Pipe (|) - used in some database exports
2. Quoted Values
Handle values containing delimiters:
name,description
Product A,"A product with, comma"
Product B,"Another ""quoted"" value"
3. Type Conversion
CSV stores everything as text. Convert appropriately:
- Numbers: "30" → 30
- Booleans: "true" → true
- Null values: "" → null
- Dates: "2023-01-15" → Date object or ISO string
4. Missing Values
Handle incomplete data:
name,age,city
John,30,New York
Jane,,Boston // missing age
Bob,35, // missing city
Best Practices
- Validate CSV format: Ensure consistent columns across all rows
- Handle headers properly: Use first row as keys or define custom keys
- Convert data types: Don't keep everything as strings
- Handle special characters: Properly parse quoted and escaped values
- Check encoding: Use UTF-8 for international characters
- Validate output: Ensure resulting JSON is valid
- Consider size: Large CSV files may need streaming
Reverse Conversion
Need to go the other way? Check out our guide on JSON to CSV conversion.
Use Cases
- API Integration: Convert CSV exports to JSON for API consumption
- Web Applications: Use CSV data in JavaScript applications
- NoSQL Databases: Import CSV data into MongoDB, CouchDB
- Data Processing: Transform CSV for analysis tools
- Configuration: Convert spreadsheet configs to JSON