Last modified: June 06, 2026
This article is written in: 🇺🇸
YAML stands for YAML Ain’t Markup Language.
It is a human-friendly data serialization format commonly used for:
A useful way to think about YAML:
YAML is designed for humans to write and read structured data with minimal syntax.
Compared to JSON, YAML is often easier to read because it relies on indentation instead of braces and commas.
employee:
id: E1001
name: Amina Rahman
active: true
department: Engineering
This means:
employee is an objectit contains fields:
id
nameactivedepartment{
"employee": {
"id": "E1001",
"name": "Amina Rahman",
"active": true,
"department": "Engineering"
}
}
YAML is built mainly from:
Like a dictionary / object:
name: Amina
department: Engineering
active: true
skills:
- Python
- SQL
- Docker
Equivalent JSON:
{
"skills": ["Python", "SQL", "Docker"]
}
Examples:
name: Amina
age: 30
active: true
salary: 50000.75
manager: null
Scalars can be:
flowchart TD
A["employee"] --> B["id: E1001"]
A --> C["name: Amina Rahman"]
A --> D["department: Engineering"]
A --> E["skills"]
E --> F["Python"]
E --> G["SQL"]
E --> H["Docker"]
Indentation defines structure in YAML.
This is valid:
employee:
name: Amina
department: Engineering
This is invalid:
employee:
name: Amina
department: Engineering
YAML uses whitespace as syntax.
That means:
Usually:
2 spaces
or
4 spaces
per nesting level.
employee:
name: Amina
address:
street: 12 Main Street
city: Berlin
country: Germany
flowchart TD
A["employee"] --> B["name"]
A --> C["address"]
C --> D["street"]
C --> E["city"]
C --> F["country"]
Very common in YAML.
Example:
employees:
- id: E1001
name: Amina
department: Engineering
- id: E1002
name: Jonas
department: Finance
Equivalent JSON:
{
"employees": [
{
"id": "E1001",
"name": "Amina"
},
{
"id": "E1002",
"name": "Jonas"
}
]
}
YAML handles long text very well.
|)Preserves line breaks:
message: |
Hello team,
Deployment completed successfully.
Monitoring is active.
Output:
Hello team,
Deployment completed successfully.
Monitoring is active.
>)Turns newlines into spaces.
message: >
Hello team,
deployment completed successfully,
monitoring is active.
Output:
Hello team, deployment completed successfully, monitoring is active.
Unlike JSON, YAML supports comments.
# Application configuration
server:
port: 8080
Comments are ignored by parsers.
A powerful YAML feature.
Useful when repeating values.
Example:
defaults: &defaults
timeout: 30
retries: 5
serviceA:
<<: *defaults
url: api.example.com
serviceB:
<<: *defaults
url: auth.example.com
Expanded meaning
serviceA:
timeout: 30
retries: 5
url: api.example.com
serviceB:
timeout: 30
retries: 5
url: auth.example.com
YAML is heavily used in Docker Compose.
Example:
version: "3"
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_DB: app
POSTGRES_USER: admin
YAML is the standard format for Kubernetes manifests.
Example:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
flowchart LR
A["YAML file"] --> B["Parser"]
B --> C["Native object"]
C --> D["Application uses values"]
Example:
port: 8080
debug: true
becomes:
Python:
{
"port": 8080,
"debug": True
}
Install parser:
pip install pyyaml
app:
name: Inventory Service
port: 8080
debug: true
import yaml
with open("config.yaml", "r") as f:
config = yaml.safe_load(f)
print(config["app"]["name"])
print(config["app"]["port"])
Expected output
Inventory Service
8080
Very common.
Bad:
app:
name: Inventory
Good:
app:
name: Inventory
YAML may auto-convert values.
Example:
enabled: yes
might become:
True
Safer:
enabled: "yes"
if string intended.
date: 2025-05-24
may parse as a date object instead of string.
Safer:
date: "2025-05-24"
Tabs can break parsing.
Always use spaces.
Avoid:
yaml.load(...)
Prefer:
yaml.safe_load(...)
| Feature | YAML | JSON |
| Human readability | Excellent | Good |
| Supports comments | Yes | No |
| Uses indentation | Yes | No |
| Uses braces/brackets | Optional | Required |
| Parsing complexity | Higher | Lower |
| Common for configs | Very common | Common |
| Common for APIs | Less common | Very common |
Prefer:
2 spaces
Example:
version: "1.0"
Deep nesting becomes difficult to read.
Helpful for config files.
safe_loadSafer when parsing user-provided YAML.
mindmap
root((YAML))
Mappings
Lists
Scalars
Comments
Anchors
Multi-line text
Config files
Docker Compose
Kubernetes
Use YAML when:
Good examples:
Avoid YAML when:
In those cases JSON may be simpler.