Guide

CSV vs JSON: Key Differences, Examples, and Use Cases

CSV and JSON store data differently. Learn when each format wins, see side-by-side examples, and pick the right one for spreadsheets, APIs, and more.

You have a dataset and need to store it, send it, or feed it into another tool. Someone says “just use CSV,” someone else says “use JSON.” Both are plain text, both are everywhere, and picking the wrong one means extra work later: reformatting, writing conversion scripts, or dealing with data that doesn’t fit the format.

Whenever I start a project with data pipelines, clients or teammates inevitably ask whether we should standardize on CSV or JSON. Here is my side-by-side breakdown of how both formats work, where each one wins in practice, and how to convert between them without corrupting data.

What is CSV?

CSV stands for Comma-Separated Values. A CSV file is a plain-text file where each line is a record and each field within a record is separated by a comma. The first line is usually a header row that names the columns.

id,name,email,plan
1,Alice,[email protected],free
2,Bob,[email protected],pro
3,Carol,[email protected],free

That is the entire format. No brackets, no braces, no indentation. Every record has the same number of fields, and every field that contains a comma, a double quote, or a line break must be wrapped in double quotes. The quoting rules come from RFC 4180, which is the closest thing CSV has to a formal specification.

CSV has been around since the early days of personal computing. Spreadsheet programs, databases, analytics platforms, and nearly every programming language can read and write it without installing anything extra.

What is JSON?

JSON stands for JavaScript Object Notation. It is a text-based format that represents data as key-value pairs, arrays, and nested structures. A JSON file that holds the same three records looks like this:

[
  {
    "id": 1,
    "name": "Alice",
    "email": "[email protected]",
    "plan": "free"
  },
  {
    "id": 2,
    "name": "Bob",
    "email": "[email protected]",
    "plan": "pro"
  },
  {
    "id": 3,
    "name": "Carol",
    "email": "[email protected]",
    "plan": "free"
  }
]

JSON distinguishes between strings, numbers, booleans, null, objects, and arrays. It can represent trees and nested data that CSV cannot express without workarounds. The specification is short and strict — json.org fits on one page — which is why parsers exist in every language and rarely disagree.

JSON became the default data format on the web because JavaScript can parse it natively, but it is now used far beyond the browser: configuration files, NoSQL databases, message queues, and machine-learning pipelines all use it.

Side-by-side comparison

CSV stores data in flat rows and columns; JSON uses nested key-value pairs that repeat keys in every object

The table below covers the differences that matter most when you are choosing between them.

Feature CSV JSON
Structure Flat table: rows and columns Nested: objects, arrays, and mixed types
Data types Everything is text; the reader decides what is a number Strings, numbers, booleans, null, objects, arrays
Readability Easy to scan in a text editor or spreadsheet Easy to read in a code editor; harder to scan for many rows
File size Compact — column names appear once in the header Larger — every key is repeated in every object
Nesting Not supported without flattening or convention Built in
Schema Implicit in the header row None built in, but JSON Schema can validate it
Tooling Spreadsheets, SQL databases, data pipelines APIs, web apps, NoSQL databases, config files
Specification RFC 4180 (informal, widely followed) ECMA-404 / RFC 8259 (strict, universal)
Streaming Easy — read one line at a time Harder — the standard format is one big array
Encoding Usually UTF-8, sometimes with a BOM UTF-8 required by the specification

When CSV is the right choice

CSV wins when your data is flat and the tool at the other end expects rows and columns.

Spreadsheets and business users

Double-click a .csv file and it opens in Excel, Google Sheets, or LibreOffice Calc. No import wizard, no code, no parsing step. If the people who receive the data work in a spreadsheet, CSV is the path of least resistance. JSON requires a conversion step before it lands in a grid.

Large flat datasets

A CSV file with 500,000 rows and 12 columns is straightforward to process. It is compact because the column names appear only once. The same data in JSON can be two to five times larger, because every object repeats all twelve keys. For bulk exports from databases, payment processors, and analytics platforms, CSV is the standard.

Database imports

Most relational databases — PostgreSQL, MySQL, SQLite, SQL Server — have a fast bulk-import command for CSV. PostgreSQL’s COPY, MySQL’s LOAD DATA INFILE, and SQLite’s .import read CSV directly. Importing JSON usually means writing a script or using a less optimized path.

Data pipelines and ETL

Tools like pandas, R, Apache Spark, and dbt read CSV natively. When your pipeline moves millions of rows between systems and none of the fields are nested, CSV keeps things simple and fast.

Logs and append-only files

Appending a line to a CSV file is trivial: open the file, write one line, close it. Appending to a JSON array requires reading the closing bracket, inserting the new object, and writing the bracket back, which is slower and more error-prone (though JSON Lines, covered below, solves this).

When JSON is the right choice

JSON wins when the data has structure beyond a flat table, or when it is headed for a web application.

APIs and web services

Nearly every modern REST API sends and receives JSON. Browsers parse it with JSON.parse(), servers serialize it with a single function call, and the format supports the request-response pattern naturally. If you are building or consuming a web API, JSON is the default.

Nested and hierarchical data

A product with an array of images, a list of variants each with their own price and stock level, and a nested address object does not fit in a flat CSV row without awkward workarounds like pipe-delimited lists inside fields or numbered column suffixes (image_1, image_2, image_3). JSON handles this natively:

{
  "id": 42,
  "name": "Wireless Keyboard",
  "price": 59.99,
  "tags": ["electronics", "peripherals"],
  "variants": [
    { "color": "black", "stock": 120 },
    { "color": "white", "stock": 45 }
  ],
  "manufacturer": {
    "name": "KeyCo",
    "country": "DE"
  }
}

Trying to flatten this into CSV means choosing between losing information, inventing a convention the reader must understand, or creating multiple CSV files with foreign keys.

Configuration files

Application settings, build configs, and infrastructure definitions often use JSON (or YAML, which is a superset of JSON). These files typically have a mix of strings, numbers, booleans, arrays, and nested objects, which maps directly to JSON’s type system. CSV has no way to express a boolean or distinguish between the number 0 and the string "0".

Document databases

MongoDB, CouchDB, Elasticsearch, and Firebase store data as JSON documents (or BSON, a binary variant). If your database is document-oriented, JSON is the native format and round-trips without conversion.

Mixed or irregular schemas

When different records have different fields — one user has a phone number and another does not, one product has dimensions and another does not — JSON handles it without empty columns. CSV requires every row to have the same number of fields, so missing values become empty cells.

When the choice is not obvious

Decision flowchart showing how to pick between CSV and JSON based on whether data is flat and where it is headed

Some situations sit in the middle. Here is how to think about them.

Flat data going to an API

The data is flat, which favors CSV, but the destination is a REST endpoint that expects JSON. In this case, store and edit the data in CSV (it is easier to work with in a spreadsheet), and convert it to JSON at the point of delivery. The CSV to JSON converter does this in your browser without uploading the file.

Nested data going to a spreadsheet

You received JSON from an API and need to put it in Excel. You have to flatten it first. Decide which nested fields you actually need, pull them into a flat structure, and write a CSV. Some tools call this “normalizing” or “unrolling.” If the nesting is deep, you may need more than one table.

Data exchange between two systems you control

If both systems are yours, pick the format that matches the data shape. Flat rows → CSV. Nested objects → JSON. If the data is flat but both systems already speak JSON, there is no reason to add a conversion step.

JSON Lines: the middle ground

Standard JSON wraps everything in a single array, which makes streaming hard and appending expensive. JSON Lines (also called NDJSON) fixes this by putting one JSON object per line, with no wrapping array:

{"id":1,"name":"Alice","email":"[email protected]","plan":"free"}
{"id":2,"name":"Bob","email":"[email protected]","plan":"pro"}
{"id":3,"name":"Carol","email":"[email protected]","plan":"free"}

This gives you JSON’s type system and nesting with CSV’s ability to stream and append. Tools like jq, DuckDB, and pandas (read_json(lines=True)) handle it well. If you need structured data in a log or a data pipeline and CSV feels too limited, JSON Lines is often the right answer.

Converting between CSV and JSON

You will eventually need to go from one format to the other. Here is how to do it without losing data.

CSV to JSON

Each row becomes a JSON object, and the header row provides the keys. There are three things to watch out for:

  1. Types. CSV does not have types, so everything arrives as a string. If you need "id" to be a number, you have to cast it after conversion. A good converter can detect obvious numbers and booleans.
  2. Empty fields. An empty cell in CSV can become an empty string "" or null in JSON, depending on the tool. Decide which you want.
  3. Encoding. If the CSV uses a byte-order mark or a non-UTF-8 encoding, clean that up first or the JSON output may contain garbled characters.

The CSV to JSON tool on this site handles all three, runs in your browser, and does not upload your file.

JSON to CSV

Flattening JSON into CSV works well when every object has the same keys and no nesting. When there is nesting, you lose structure:

  • Arrays can be joined into a delimited string ("electronics|peripherals"), but the reader needs to know the convention.
  • Nested objects can be flattened with dot notation (manufacturer.name, manufacturer.country), which creates more columns.
  • Missing keys become empty cells.

The JSON to CSV tool does this conversion in your browser. For complex nesting, a script gives you more control over how the flattening works.

Python example

This script converts a CSV file to a JSON file and preserves the original order of columns:

import csv
import json

with open("data.csv", newline="", encoding="utf-8-sig") as f:
    rows = list(csv.DictReader(f))

with open("data.json", "w", encoding="utf-8") as f:
    json.dump(rows, f, indent=2, ensure_ascii=False)

And the reverse, JSON to CSV:

import csv
import json

with open("data.json", encoding="utf-8") as f:
    rows = json.load(f)

with open("data.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=rows[0].keys())
    writer.writeheader()
    writer.writerows(rows)

Both scripts handle UTF-8 and byte-order marks correctly. The ensure_ascii=False argument keeps accented characters as-is instead of escaping them.

Common mistakes

  • Assuming CSV fields are typed. A column of numbers in CSV is still text until the reader parses it. Leading zeros in codes like 007 survive in CSV but may be stripped if a tool interprets the field as a number.
  • Putting JSON arrays in CSV cells. Storing ["red","blue"] inside a CSV field works, but the outer tool sees it as a plain string. Anyone downstream has to parse JSON inside CSV, which defeats the purpose of both formats.
  • Ignoring file size. A 50 MB CSV can become 150 MB as JSON. If the data is flat and you are sending it over the network or storing millions of rows, the size difference matters.
  • Using CSV for configs. A configuration file with booleans, nested sections, and optional keys does not fit in a table. Use JSON (or YAML, or TOML).
  • Using JSON for spreadsheet users. If the person receiving the file will open it in Excel, send CSV. Asking a non-technical user to deal with JSON adds friction and errors.

File size comparison

To give a concrete sense of the difference, here is a rough comparison for a flat dataset with 10 columns and average field lengths:

Records CSV (approx.) JSON (approx.) JSON Lines (approx.)
1,000 85 KB 200 KB 190 KB
100,000 8.5 MB 20 MB 19 MB
1,000,000 85 MB 200 MB 190 MB

The ratio depends on how long the keys and values are. Short keys and long values bring the formats closer together. Short values and long keys make JSON relatively larger. Compression (gzip, zstd) reduces both formats significantly and narrows the gap, because repeated keys compress well.

Quick reference: CSV or JSON?

Scenario Best format
Exporting data for a spreadsheet CSV
Sending data to a REST API JSON
Bulk database import CSV
App configuration JSON
Flat data with millions of rows CSV
Nested or hierarchical data JSON
Log files you append to CSV or JSON Lines
Data exchange with mixed schemas JSON
Sharing data with non-technical users CSV
Frontend/backend communication JSON

Tools on this site

These tools convert between the two formats in your browser, with no upload and no server processing:

  • CSV to JSON — turns a CSV file into a JSON array of objects.
  • JSON to CSV — flattens a JSON array into a downloadable CSV.
  • CSV Viewer — inspect a CSV file before converting it.
  • CSV to Excel — when the destination is a spreadsheet, not an API.

If you are working with files that have quoting problems — commas or line breaks inside fields — fix those before converting. See our guide on fixing comma and newline errors in CSV fields. For files with sensitive data, read why you shouldn’t upload CSVs to cloud converters before choosing a tool.

Frequently asked questions

Is CSV or JSON better for large datasets?

For flat, tabular data with millions of rows, CSV is usually better. It is smaller because it does not repeat field names, and tools like Excel, pandas, and databases import it faster. JSON works better when rows have different fields or when you need nested structures like arrays inside each record.

Can JSON replace CSV completely?

Not in every case. JSON can represent anything CSV can, but CSV is more compact for flat tables, opens directly in spreadsheets, and loads faster in data pipelines. If your workflow centers on spreadsheets or SQL databases, CSV is still the simpler choice.

How do I convert CSV to JSON?

Use a browser-based converter like the CSV to JSON tool on this site, a script in Python or JavaScript, or a command-line tool like jq combined with csvkit. The converter reads the header row as keys and turns each data row into a JSON object.

Why does JSON use more disk space than CSV for the same data?

JSON repeats the key name in every object. In a file with 10 columns and 100,000 rows, those keys appear 1,000,000 times. CSV writes the column names once in the header, so the same data is often 2 to 5 times smaller in CSV format.

Which format do APIs usually accept?

Most modern REST and GraphQL APIs accept and return JSON. JSON's support for nesting, typed values, and standard parsing in every browser makes it the default for web services. Some bulk-import APIs accept CSV because it is more compact for large uploads.