How to Convert Between CSV and JSON

csvjsondata

CSV and JSON are two of the most common ways to move data between systems, but they are optimized for very different shapes of data. Knowing which one to use saves a lot of cleanup later.

CSV versus JSON

  • CSV is tabular and flat. Every row has the same columns, and it is great for spreadsheets and bulk exports.
  • JSON is nested and flexible. It can represent objects, arrays and hierarchies, which makes it the default for APIs.

When to use each

Use CSV when the data is a simple table and humans or spreadsheet tools will consume it. Use JSON when the data has structure or is exchanged between applications.

Converting in code

JavaScript:

const rows = [{ name: 'Ada', email: '[email protected]' }]
const json = JSON.stringify(rows, null, 2)

Python:

import csv, io, json
rows = [{'name': 'Ada', 'email': '[email protected]'}]
with io.StringIO() as out:
    writer = csv.DictWriter(out, fieldnames=['name', 'email'])
    writer.writeheader()
    writer.writerows(rows)
    csv_text = out.getvalue()

PHP:

$rows = [['name' => 'Ada', 'email' => '[email protected]']];
echo json_encode($rows, JSON_PRETTY_PRINT);

Common pitfalls

  • Assuming the first row is always a header when it is not.
  • Ignoring quoted fields that contain commas or newlines.
  • Losing nested structure when flattening JSON into CSV.

Frequently asked questions

  • Which delimiters are supported? Common ones are comma, semicolon, tab and pipe.
  • Is my data uploaded? No. Conversion can run entirely in your browser.

Need to move between CSV and JSON? Try the free CSV to JSON Converter — it runs locally with configurable delimiters.