esc

JSON vs CSV vs XML: which to use and how to convert

A plain-English comparison of JSON, CSV and XML: what each is good at, when to pick which, and how to convert between them without losing data.

· Treeet

JSON, CSV and XML are three of the most common ways to store and exchange data. You’ll meet them when exporting from an app, calling an API, importing into a spreadsheet or moving data between systems. They can often hold the same information, but each is built for a different job.

This guide explains the differences in plain terms, helps you choose, and shows how to convert between them.

The same data, three ways

Here’s a small list of customers in each format.

CSV (comma-separated values):

id,name,city
1,Ana,Lisbon
2,Ben,Toronto

JSON (JavaScript Object Notation):

[
  { "id": 1, "name": "Ana", "address": { "city": "Lisbon" } },
  { "id": 2, "name": "Ben", "address": { "city": "Toronto" } }
]

XML (Extensible Markup Language):

<customers>
  <customer id="1">
    <name>Ana</name>
    <city>Lisbon</city>
  </customer>
  <customer id="2">
    <name>Ben</name>
    <city>Toronto</city>
  </customer>
</customers>

CSV: flat tables

CSV is a table in plain text. The first line usually holds column names; each line after that is a row, with values separated by commas (or sometimes semicolons or tabs).

Good for:

Weak spots:

JSON: nested data for apps and APIs

JSON stores objects (named fields) and lists, which can be nested inside each other as deeply as needed. It’s the usual format for web APIs and app settings.

Good for:

Weak spots:

XML: tagged documents

XML wraps data in opening and closing tags, and elements can also carry attributes (like id="1" above). It’s older than JSON and still widely used in feeds, office file formats, configuration files and many business and government data exchanges.

Good for:

Weak spots:

Which one should you use?

If you need to…Use
Open or edit data in a spreadsheetCSV (or Excel)
Send data to or from a web APIJSON
Store nested or hierarchical dataJSON
Feed a system that requires XMLXML
Share a simple list with non-technical peopleCSV or Excel
Keep app or tool configuration readableJSON or YAML

In practice, the system on the other end usually decides. When you have a free choice: CSV for tables, JSON for anything nested.

How to convert between them

All of these converters run in your browser, so your data stays on your device. You can convert several files at once.

JSON to CSV

Use the JSON to CSV converter. It handles the common cases sensibly:

CSV to JSON

The CSV to JSON converter turns each row into an object, using the first row as field names. Blank column names become column1, column2 and so on, and duplicate names get a number added so nothing is lost.

With Detect numbers & booleans on (the default), 42 becomes the number 42, true becomes true, and empty cells become null. Values with leading zeros, such as 007, stay as text, which protects ZIP codes and IDs. You can also choose the indent: 2 spaces, 4 spaces or minified.

XML to JSON

The XML to JSON converter checks that the XML is valid first and tells you the line and column of any error. In the result:

JSON to XML

The JSON to XML converter does the reverse. If your JSON has a single top-level field holding an object, that field becomes the root element. Otherwise everything is wrapped in a <root> element, and items of a top-level list become <item> elements. Field names that aren’t valid in XML are adjusted so the file stays valid.

XML to CSV

There’s no one-step XML to CSV converter, but two steps get you there: convert XML to JSON, then convert that JSON to CSV. Repeated elements become rows and nested elements become dotted columns.

Spreadsheets and YAML

Common pitfalls

Pick the format that fits the job, convert when you need to, and check a few rows of the result before you rely on it.

More guides

How to turn bank statement PDFs into CSV for budgetingGet the transactions out of your bank statement PDFs and into a spreadsheet or budgeting app, without uploading your statements anywhere.How to convert iPhone HEIC photos to JPGTurn iPhone HEIC photos into JPGs that open anywhere, right in your browser, and change one iPhone setting so new photos are saved as JPG.How to merge, split and compress PDFs without uploadingCombine PDFs, pull out pages and shrink big files for email or upload forms, using free tools that run in your browser so your documents stay private.