Skip to content
UtilHQ
guides

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.

By UtilHQ Team
Ad Space

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:

  1. Rows are separated by line breaks. Either \n (Unix) or \r\n (Windows).
  2. Fields are separated by a delimiter. Usually a comma, but semicolons are common in European locales where commas serve as decimal separators.
  3. The first row is typically a header containing column names, but this is a convention, not a requirement.
  4. Fields containing the delimiter, quotes, or newlines must be enclosed in double quotes. For example: "Portland, OR".
  5. Double quotes inside quoted fields are escaped by doubling them. The value She said "hello" becomes "She said ""hello""" in CSV.

Common delimiters:

DelimiterNameWhen Used
,CommaDefault in US/UK, most software
;SemicolonEuropean Excel exports
\tTabTSV files, database exports
|PipeData 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.street and address.city become column headers
  • JSON string: The entire address object 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:

  1. Export table to CSV from MySQL, PostgreSQL, or SQLite
  2. Convert CSV to JSON
  3. POST each object to the API endpoint
  4. Handle errors and log failures

Spreadsheet to application:

  1. Export Google Sheets or Excel to CSV
  2. Clean the data (remove empty rows, trim whitespace)
  3. Convert to JSON with type inference
  4. Import into the application

API response to spreadsheet:

  1. Fetch JSON from API
  2. Flatten nested objects
  3. Convert to CSV
  4. 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.

Share this article

Have suggestions for this article?