How to Convert CSV to JSON (And Back Again)
Learn CSV and JSON structure, when to use each format, and how to handle delimiters, quoted fields, header rows, and type inference when converting between them.
CSV and JSON are two of the most common data interchange formats. CSV dominates spreadsheets, database exports, and legacy systems. JSON dominates APIs, configuration files, and modern web applications. Knowing how to move data between them is a routine task for anyone working with data.
This guide covers the structure of both formats, the gotchas that trip up conversions, and when to use each one. For instant conversion, try our CSV to JSON Converter or convert the other direction with JSON to CSV.
CSV Format Basics
CSV stands for Comma-Separated Values. Despite the name, the format is surprisingly loose. There’s no single official standard, though RFC 4180 provides the closest thing to a specification.
A basic CSV file looks like this:
name,age,city
Alice,30,Portland
Bob,25,Seattle
Carol,35,Denver
Key rules:
- Rows are separated by line breaks. Either
\n(Unix) or\r\n(Windows). - Fields are separated by a delimiter. Usually a comma, but semicolons are common in European locales where commas serve as decimal separators.
- The first row is typically a header containing column names, but this is a convention, not a requirement.
- Fields containing the delimiter, quotes, or newlines must be enclosed in double quotes. For example:
"Portland, OR". - Double quotes inside quoted fields are escaped by doubling them. The value
She said "hello"becomes"She said ""hello"""in CSV.
Common delimiters:
| Delimiter | Name | When Used |
|---|---|---|
, | Comma | Default in US/UK, most software |
; | Semicolon | European Excel exports |
\t | Tab | TSV files, database exports |
| | Pipe | Data with commas and semicolons in values |
JSON Structure
JSON (JavaScript Object Notation) is a text-based format that represents structured data as key-value pairs and ordered lists.
The same data in JSON:
[
{ "name": "Alice", "age": 30, "city": "Portland" },
{ "name": "Bob", "age": 25, "city": "Seattle" },
{ "name": "Carol", "age": 35, "city": "Denver" }
]
JSON data types: strings, numbers, booleans, null, arrays, and nested objects. That’s a major difference from CSV, where everything is a string and type interpretation is left to the reader.
JSON rules:
- Keys must be strings enclosed in double quotes
- Strings use double quotes (not single quotes)
- No trailing commas after the last element
- No comments (unlike YAML or JSONC)
- Unicode support is built in
When to Use Each Format
Choose CSV when:
- Data is flat (no nesting) with consistent columns
- The consumer is a spreadsheet, database import, or legacy system
- File size matters (CSV is more compact for tabular data)
- Non-technical users need to read or edit the file
- You are exporting data for reporting or analysis
Choose JSON when:
- Data has nested or hierarchical structure
- The consumer is a web API or modern application
- You need typed values (numbers, booleans, null)
- Data has variable fields per record
- You are building configuration files or data payloads
How CSV to JSON Conversion Works
The conversion process maps CSV rows to JSON objects using the header row as keys.
Step 1: Parse the header row.
name,age,city → ["name", "age", "city"]
Step 2: Parse each data row into an object.
Alice,30,Portland → { "name": "Alice", "age": "30", "city": "Portland" }
Step 3: Collect all objects into an array.
[
{ "name": "Alice", "age": "30", "city": "Portland" },
{ "name": "Bob", "age": "25", "city": "Seattle" }
]
Step 4 (optional): Type inference.
Convert string values to appropriate types: "30" becomes 30 (number), "true" becomes true (boolean), "" becomes null. Type inference is helpful but can misfire. The ZIP code "01234" should stay a string, not become the number 1234.
Handling Tricky CSV Fields
Quoted Fields with Commas
name,address,city
"Smith, John","123 Main St, Apt 4",Portland
The parser must recognize that "Smith, John" is a single field, not two fields split at the comma. Any RFC 4180-compliant parser handles this, but naive comma-splitting (like line.split(',')) won’t work.
Embedded Newlines
name,notes
Alice,"Line one
Line two"
Bob,"Single line"
The value for Alice’s notes spans two lines. A parser that splits on newlines before handling quotes will corrupt this data. Always parse quotes first, then split.
Escaped Quotes
name,quote
Alice,"She said ""hello"" to everyone"
The doubled quotes "" represent a literal " character. The JSON output should be:
{ "name": "Alice", "quote": "She said \"hello\" to everyone" }
Missing Values
name,age,city
Alice,,Portland
Bob,25,
Empty fields can become empty strings, null, or be omitted depending on your conversion strategy. Define a consistent rule before processing.
How JSON to CSV Conversion Works
Converting JSON back to CSV requires flattening the data.
Flat JSON converts directly:
{ "name": "Alice", "age": 30 } → Alice,30
Nested JSON requires decisions:
{
"name": "Alice",
"address": {
"street": "123 Main St",
"city": "Portland"
}
}
Common flattening strategies:
- Dot notation:
address.streetandaddress.citybecome column headers - JSON string: The entire
addressobject is serialized as a string in one column - Ignore: Nested objects are dropped
Arrays in JSON create similar challenges. A field containing ["red", "blue"] might become "red;blue" (joined string) or get split into separate columns.
Data Migration Workflows
When moving data between systems, CSV-to-JSON conversion is often one step in a larger pipeline.
Database export to API:
- Export table to CSV from MySQL, PostgreSQL, or SQLite
- Convert CSV to JSON
- POST each object to the API endpoint
- Handle errors and log failures
Spreadsheet to application:
- Export Google Sheets or Excel to CSV
- Clean the data (remove empty rows, trim whitespace)
- Convert to JSON with type inference
- Import into the application
API response to spreadsheet:
- Fetch JSON from API
- Flatten nested objects
- Convert to CSV
- Open in Excel or Google Sheets
Tips for reliable migrations:
- Validate row counts before and after conversion
- Spot-check a few records manually
- Handle encoding issues (UTF-8 BOM, Latin-1 characters)
- Log any rows that fail parsing
Convert Your Data Now
Our CSV to JSON Converter handles delimiters, quoted fields, and type inference automatically. Going the other way? The JSON to CSV Converter flattens nested objects and generates clean, downloadable CSV files.
Frequently Asked Questions
What happens to nested JSON when converting to CSV?
CSV is inherently flat, so nested objects and arrays have to be flattened. The most common approach is dot notation, where address.city becomes a column header. Arrays can be joined into a single string with a separator or expanded into multiple columns. Our converter uses dot notation for nested objects and joins arrays with semicolons.
Why does my CSV file have semicolons instead of commas?
European versions of Excel use semicolons as the default CSV delimiter because commas are used as decimal separators in many European countries (e.g., 1.000,50 instead of 1,000.50). When converting, specify the delimiter explicitly rather than assuming commas. Most converters let you choose between comma, semicolon, tab, and pipe.
How do I handle CSV files with no header row?
If the first row contains data rather than column names, you need to either provide column names manually or use auto-generated names like column_1, column_2, etc. Most conversion tools have an option to specify whether the first row is a header. Without headers, the JSON output can use an array of arrays instead of an array of objects.
Can I convert a CSV file with millions of rows to JSON?
Large files require streaming rather than loading everything into memory. A 1 million row CSV might be 100 MB as text but 300+ MB as a JSON array of objects due to repeated key names. For large datasets, consider JSON Lines (JSONL) format, where each line is a separate JSON object. This allows line-by-line processing without loading the entire file.
What encoding should my CSV file use?
UTF-8 is the safest choice and handles characters from virtually every language. If your file was exported from Excel on Windows, it might use Windows-1252 encoding or include a UTF-8 BOM (byte order mark, the bytes EF BB BF at the start of the file). Remove the BOM before parsing, or use a converter that handles it automatically. Characters appearing as garbled text (like é instead of e) are a sign of encoding mismatch.
Related Calculators
Related Articles
- How to Fix Line Breaks from PDF Copy-Paste
Fix unwanted line breaks when copying text from PDFs. Learn why PDFs add breaks, how to remove them, and techniques for cleaning up formatted text quickly.
- How to Calculate Body Fat Percentage (Navy Method)
Learn the U.S. Navy body fat formula with step-by-step examples, body fat category charts, measurement tips, and comparisons to DEXA, calipers, and other methods.
- How to Calculate Daily Calories for Your Goals
Learn the Mifflin-St Jeor equation, activity multipliers, and how to set calorie targets for weight loss, maintenance, or muscle gain. Includes worked examples.
- How to Calculate Ideal Weight (4 Formulas Compared)
Compare the Devine, Robinson, Miller, and Hamwi ideal weight formulas with worked examples. Learn why ideal weight varies and what these numbers actually mean.
Share this article
Have suggestions for this article?