YAML vs JSON
Both are data serialization formats, but they serve different purposes best.
Syntax Comparison
JSON:
{
"server": {
"host": "localhost",
"port": 8080,
"ssl": true
},
"databases": ["mysql", "redis"]
}
YAML:
server:
host: localhost
port: 8080
ssl: true
databases:
- mysql
- redis
Key Differences
| Comments | ❌ Not supported | ✅ # comments |
|---|
| Verbosity | More punctuation | Minimal |
|---|
| Parsing speed | Faster | Slower |
|---|
When to Use JSON
- APIs: Standard for REST APIs
- Data exchange: Universal support
- JavaScript apps: Native parsing
- Performance critical: Faster parsing
When to Use YAML
- Configuration files: More readable
- Docker Compose: Standard format
- Kubernetes: All manifests are YAML
- CI/CD pipelines: GitHub Actions, GitLab CI
YAML Superpowers
# Multi-line strings
description: |
This is a multi-line
string that preserves
line breaks.
# Anchors and aliases (DRY)
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults
host: prod.example.com