JSON vs XML vs YAML vs CSV
JSON isn't the only way to store and exchange data. XML, YAML and CSV all solve similar problems, and you'll meet each of them in real projects. Seeing the same data in all four formats is the quickest way to understand what each is good at.
The same data in four formats
Here's a small list of books, written four ways.
{
"books": [
{ "title": "Dune", "author": "Frank Herbert", "year": 1965, "inStock": true },
{ "title": "Kindred", "author": "Octavia E. Butler", "year": 1979, "inStock": false }
]
}<?xml version="1.0" encoding="UTF-8"?>
<books>
<book inStock="true">
<title>Dune</title>
<author>Frank Herbert</author>
<year>1965</year>
</book>
<book inStock="false">
<title>Kindred</title>
<author>Octavia E. Butler</author>
<year>1979</year>
</book>
</books>books:
- title: Dune
author: Frank Herbert
year: 1965
inStock: true
- title: Kindred
author: Octavia E. Butler
year: 1979
inStock: falsetitle,author,year,inStock
Dune,Frank Herbert,1965,true
Kindred,Octavia E. Butler,1979,falseJSON
JSON (JavaScript Object Notation) has a tiny set of rules: objects, arrays, strings, numbers, booleans and null. That small size is its biggest strength. Every major programming language can read and write it, usually with a built-in library, and values come out with their types intact: 1965 is a number and true is a boolean, with no guessing.
- Best for: web APIs, data sent between programs, JavaScript apps, document databases, and app settings that programs write.
- Weak spots: no comments, strict syntax that's easy to break when editing by hand (see common JSON errors), and no built-in date type.
XML
XML (eXtensible Markup Language) uses tags like HTML. It was the standard data exchange format before JSON, and it's still everywhere in enterprise systems, SOAP web services, RSS feeds, Office documents (a .docx file is a zip of XML files), SVG images and Android layouts.
- Strengths: attributes and mixed content (text with tags inside it), namespaces for combining vocabularies, comments, and mature tools for validation (XSD) and querying (XPath, XSLT).
- Weak spots: verbose, since every value is wrapped in an opening and closing tag. Everything is text, so
1965is a number only if a schema says so. And there's more than one way to model the same data:inStockcould be an attribute or a child element.
YAML
YAML ("YAML Ain't Markup Language") uses indentation instead of brackets, which makes it pleasant for people to read and edit. It's the usual format for configuration in DevOps tools: Docker Compose, Kubernetes, GitHub Actions and Ansible all use it.
- Strengths: comments with
#, very little punctuation, multi-line strings, and anchors for reusing a block. YAML 1.2 is designed as a superset of JSON, so most JSON documents are also valid YAML. - Weak spots: indentation errors change the meaning of a file silently, and tabs aren't allowed for indentation. Unquoted values get their types guessed, which can surprise you: parsers that follow the older YAML 1.1 rules read a country code
NOas the booleanfalse, and in every version a software version like1.10is read as the number1.1. Quote strings when in doubt.
Converting YAML to JSON
Because YAML and JSON model the same kinds of data, converting between them is lossless apart from comments. If a YAML config isn't doing what you expect, converting it to JSON and viewing it in the JSON tool shows exactly how it was interpreted: which values are strings, which are numbers, and how deeply each key is nested.
CSV
CSV (comma-separated values) is a plain table: a header row, then one row per record. Every spreadsheet program can open it, which makes it the easiest way to hand data to people who don't write code.
- Strengths: compact for large tables, opens in Excel and Google Sheets, streams one row at a time, and is easy to import into databases.
- Weak spots: flat only. A book with several authors, or an order with a list of items, doesn't fit in one row without inventing your own convention. Every value is text with no types, and details like quoting commas inside values, line endings and character encodings vary between programs.
Side-by-side comparison
| JSON | XML | YAML | CSV | |
|---|---|---|---|---|
| Nested data | Yes | Yes | Yes | No |
| Data types | String, number, boolean, null | Text (types via schema) | Guessed from the value | Text only |
| Comments | No | Yes | Yes | No |
| Easy to edit by hand | Fair | Fair | Good | Good in a spreadsheet |
| Size | Compact | Largest | Compact | Smallest for tables |
| Typical use | APIs, app data | Documents, enterprise | Config files | Spreadsheets, exports |
Which one should you use?
- Building or calling a web API? JSON. It's the default for REST APIs and what front-end code expects.
- Writing a config file people will edit? YAML if the tool supports it and you want comments, or JSON if the tool expects it. Many tools accept both.
- Sharing a table with non-developers? CSV.
- Integrating with a system that already uses XML? XML. Don't fight an existing standard.
You'll often convert between them. A common workflow is exporting JSON from an API, flattening it into CSV for a spreadsheet, or loading a YAML config and checking it as JSON. The more nested the data, the more JSON or YAML make sense, and the more awkward CSV becomes.