Skip to main content

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

FeatureJSONXMLYAML
Comments
Native data typesPartial✗ all strings✓ incl. dates
Human readabilityHighMediumVery High
Parse speedVery FastSlowerSlowest
Schema / ValidationJSON SchemaXSD, DTDVarious
Whitespace sensitiveNoNoYes
Primary useWeb APIs, storageDocuments, SOAPConfig, 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

PitfallConsequencePrevention
Using YAML for large payloadsSlow parsing, indentation bugsUse JSON for machine-to-machine transfer
Using JSON for Kubernetes manifestsVerbose, no commentsUse YAML as intended by the ecosystem
YAML boolean coercion: yes, no, on, offCoerce to bool unexpectedlyQuote values if you mean the string
XML for REST APIsHeavy, slow, verboseDefault to JSON unless legacy requires XML

What's Next