JSON vs XML vs YAML
Learning Focus
Choose the right serialisation format by understanding the trade-offs between JSON, XML, and YAML.
Same Data, Three Formats
data.json
{
"user": {
"id": 101,
"name": "Alice",
"roles": ["admin", "editor"],
"active": true
}
}
data.xml
<user>
<id>101</id>
<name>Alice</name>
<roles>
<role>admin</role>
<role>editor</role>
</roles>
<active>true</active>
</user>
data.yaml
user:
id: 101
name: Alice
roles:
- admin
- editor
active: true
Feature Comparison
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Comments | ✗ | ✓ | ✓ |
| Native data types | Partial | ✗ all strings | ✓ incl. dates |
| Human readability | High | Medium | Very High |
| Parse speed | Very Fast | Slower | Slowest |
| Schema / Validation | JSON Schema | XSD, DTD | Various |
| Whitespace sensitive | No | No | Yes |
| Primary use | Web APIs, storage | Documents, SOAP | Config, CI/CD |
Decision Flowchart
Format Decision Guide
- Need comments or human editing?
- Yes, and it's a config or CI/CD file → YAML (Docker Compose, Kubernetes, GitHub Actions)
- Yes, and it's a legacy enterprise system → XML (SOAP, Java/Spring)
- No → JSON (Web APIs, programmatic storage)
Concept Map
Concept Flow
Data Serialisation
├── JSON → Web APIs and Databases
├── XML → Enterprise Documents
└── YAML → Config and DevOps
Common Pitfalls
| Pitfall | Consequence | Prevention |
|---|---|---|
| Using YAML for large payloads | Slow parsing, indentation bugs | Use JSON for machine-to-machine transfer |
| Using JSON for Kubernetes manifests | Verbose, no comments | Use YAML as intended by the ecosystem |
YAML boolean coercion: yes, no, on, off | Coerce to bool unexpectedly | Quote values if you mean the string |
| XML for REST APIs | Heavy, slow, verbose | Default to JSON unless legacy requires XML |
What's Next
- Previous: History and Design — Review the JSON origin story.
- Next: JSON Syntax Overview — Master the six JSON syntax rules.
- Section Overview — Return to the Introduction module index.