JSON vs XML vs YAML: Which Format Should You Use?
You've got data to store or send. Should you use JSON, XML, or YAML? The answer depends on what you're building and who'll be reading it. Let's cut through the noise and figure out which format fits your needs.
Quick Comparison Table
Here's the TL;DR version:
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Human readable | Good | Okay | Best |
| File size | Small | Large | Smallest |
| Comments | No | Yes | Yes |
| Parsing speed | Fast | Slow | Medium |
| Web APIs | Standard | Legacy | Rare |
| Config files | Common | Common | Preferred |
JSON: The Web Standard
Here's the same data in JSON:
{
"person": {
"name": "Alice",
"age": 28,
"email": "alice@example.com",
"roles": ["admin", "editor"]
}
}
Pros
- Native to JavaScript (no parsing surprises)
- Compact and lightweight
- Fast to parse in any language
- The de facto standard for REST APIs
- Every programming language has built in support
Cons
- No comments allowed
- No native date type (you use strings)
- Strict syntax can be annoying (trailing commas, double quotes)
- No support for references or anchors
Best for
REST APIs, web applications, JavaScript projects, data interchange between services. Basically anything on the web.
XML: The Enterprise Choice
Here's the same data in XML:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name>Alice</name>
<age>28</age>
<email>alice@example.com</email>
<roles>
<role>admin</role>
<role>editor</role>
</roles>
</person>
Notice how much more typing that is? That's the XML tradeoff. You get more structure, but at the cost of verbosity.
Pros
- Supports attributes on elements
- Has namespaces for avoiding conflicts
- Schema validation with XSD
- Can include comments
- Mature ecosystem with XSLT, XPath, XQuery
Cons
- Verbose (lots of repeated tags)
- Larger file sizes
- Slower to parse
- More complex to work with programmatically
- Feels dated for modern web development
Best for
SOAP APIs (legacy enterprise systems), document markup, configuration in Java applications, situations where you need strict schema validation, RSS feeds.
YAML: The Config Favorite
Here's the same data in YAML:
person:
name: Alice
age: 28
email: alice@example.com
roles:
- admin
- editor
Look at how clean that is. No brackets, no quotes (usually), just indentation. This is why YAML is popular for config files.
Pros
- Most readable of the three
- Supports comments
- Smallest file sizes
- Multi document files (useful for Kubernetes)
- Anchors and aliases for reusing content
Cons
- Indentation is significant (tabs vs spaces issues)
-
Some surprising behaviors (Norway problem:
NObecomesfalse) - Multiple ways to write the same thing
- Not ideal for data interchange (use JSON)
- Slower to parse than JSON
Best for
Config files (Docker Compose, Kubernetes, GitHub Actions, CI/CD pipelines), human edited files, documentation with embedded data.
When to Use Each One
Choose JSON when:
- Building a REST API
- Sending data between services
- Working with JavaScript/TypeScript
- You need fast parsing
- The data will be machine generated and machine consumed
Choose XML when:
- Working with legacy enterprise systems
- You need document markup (like HTML)
- Schema validation is critical
- You need attributes on elements
- The industry standard requires it (banking, healthcare)
Choose YAML when:
- Writing configuration files
- Humans will read and edit the file frequently
- You need comments in your data
- Working with DevOps tools (Ansible, Kubernetes, Docker)
- File size matters and data is relatively simple
The practical reality
Most developers use all three depending on context. Your API probably returns JSON, your Docker setup uses YAML, and that old integration with the bank uses XML. That's fine. Use the right tool for the job.
Converting Between Formats
Need to convert between these formats? It's usually straightforward since they all represent similar data structures.
Online tools
There are plenty of converters online. Just search for "JSON to YAML converter" or similar. Be careful with sensitive data though.
Command line
Tools like yq (YAML processor) and jq (JSON processor) can
convert between formats:
# YAML to JSON
yq -o=json input.yaml > output.json
# JSON to YAML
yq -P input.json > output.yaml
In code
Most languages have libraries for all three formats:
// JavaScript
const yaml = require('js-yaml');
const jsonData = JSON.parse(jsonString);
const yamlString = yaml.dump(jsonData);
# Python
import json
import yaml
data = json.loads(json_string)
yaml_string = yaml.dump(data)