How to Convert CSV to JSON — Step-by-Step Guide
CSV (Comma-Separated Values) and JSON (JavaScript Object Notation) are two of the most common data interchange formats. Whether you're migrating data between systems, preparing data for a web API, or simply reformatting a spreadsheet, converting CSV to JSON is a skill every developer and data analyst needs. This guide walks you through the process step by step.
Understanding the Formats
CSV is a flat, tabular format where each line represents a row and values are separated by commas. The first line typically contains column headers. CSV is lightweight and universally supported by spreadsheets and databases.
name,age,email Alice,30,alice@example.com Bob,25,bob@example.com
JSONis a hierarchical format that represents data as key-value pairs and nested structures. It's the native format of most web APIs and supports arrays, objects, and nested objects — making it more expressive than CSV.
[ { "name": "Alice", "age": 30, "email": "alice@example.com" }, { "name": "Bob", "age": 25, "email": "bob@example.com" } ]
Step-by-Step Conversion Process
Converting CSV to JSON involves three core steps:
- Parse the header row — Extract column names from the first line. These become the JSON keys.
- Iterate over data rows — For each subsequent line, split the values by the delimiter (usually a comma).
- Map headers to values — Pair each column name with its corresponding value and create a JSON object. Collect all objects into an array.
Handling Edge Cases
Real-world CSV files are rarely clean. Here are common issues to watch for:
- Quoted values: A field like
"New York, NY"contains a comma inside quotes. A proper parser respects quote boundaries. - Escaped quotes: Fields containing the quote character itself use double-quotes as escape, like
"She said ""hello""" - Missing values: Empty fields like
Alice,,alice@example.comshould map tonullor an empty string. - Type coercion: JSON distinguishes between numbers, strings, and booleans. Decide whether to keep everything as strings or auto-detect types.
Conversion in JavaScript
Here is a simple function that converts a CSV string to JSON in JavaScript:
function csvToJson(csv) {
const lines = csv.trim().split('\n')
const headers = lines[0].split(',')
return lines.slice(1).map(line => {
const values = line.split(',')
return headers.reduce((obj, header, i) => {
obj[header.trim()] = values[i]?.trim() ?? ''
return obj
}, {})
})
}For production use, consider a dedicated library like papaparse which handles quoted fields, different delimiters, and type detection automatically.
When to Use CSV vs JSON
Both formats have their place. CSV excels at representing flat, tabular data compactly — spreadsheets export it by default and it loads fast. JSON is better for nested data, web APIs, and configuration files. Choose based on your data structure and the tools consuming it.
Try It Online
Convert Your CSV to JSON Instantly
Paste your CSV data into ToolStack's free converter and get properly formatted JSON in seconds. No file upload needed — everything runs in your browser.
Related Tools
CSV to JSON Converter
Convert CSV data to JSON format with proper typing and formatting. Free and client-side.
JSON to CSV Converter
Transform JSON arrays into clean CSV files ready for spreadsheets and databases.
CSV Formatter
Beautify and validate CSV data with proper quoting, delimiters, and consistent formatting.