YAML
YAML (Yet Another Markup Language / YAML Ain't Markup Language) Breakdown
YAML is a human-readable data serialization format commonly used for configuration files, data exchange, and infrastructure-as-code (e.g., Docker Compose, Kubernetes, Ansible).
Unlike XML and JSON, YAML is designed to be clean and minimal, using indentation instead of brackets or tags.
1. Basic YAML Structure
A YAML document consists of key-value pairs, lists, and nested structures, all defined using indentation.
Example YAML File
name: Alice
age: 30
is_student: false
hobbies:
- Reading
- Gaming
- Hiking
address:
city: New York
zip: "10001"
Key Features:
- Key-Value Pairs (
name: Alice) - Boolean Values (
true/false) - Lists (Arrays) (
hobbies:with-for each item) - Nested Structures (
address:hascityandzip)
π¨ YAML is indentation-sensitive! Always use spaces, not tabs.
2. YAML Syntax Rules
-
Key-value pairs are separated by
:key: value -
Indentation determines structure (No brackets/commas)
person: name: Bob age: 25 -
Lists use
-(hyphen)hobbies: - Reading - Gaming -
Strings donβt need quotes unless necessary
message: Hello, world!-
Use quotes if the value contains
:or special characters:url: "https://example.com"
-
-
Booleans, Numbers, and Null
active: true count: 42 empty_value: null -
Multi-line Strings
-
Literal block (
|) (Preserves new lines)description: | This is a multi-line string in YAML. -
Folded block (
>) (Joins lines with spaces)description: > This is a folded multi-line string.
-
3. YAML Data Types
| Type | Example |
|---|---|
| String | name: "Alice" |
| Integer | age: 30 |
| Float | price: 10.99 |
| Boolean | is_student: false |
| Null | value: null |
| List | hobbies: [Reading, Gaming] |
| Dictionary | address: {city: New York, zip: 10001} |
4. Lists in YAML
Inline List
fruits: [Apple, Banana, Cherry]
Multiline List
fruits:
- Apple
- Banana
- Cherry
5. Dictionaries (Objects)
YAML supports key-value nesting like JSON.
Inline Dictionary
person: {name: Alice, age: 30}
Multiline Dictionary
person:
name: Alice
age: 30
6. Anchors & Aliases (Reuse Data)
YAML allows reusing values using & (anchor) and * (alias).
defaults: &default_settings
timeout: 30
retries: 5
server:
<<: *default_settings
host: example.com
&default_settingsdefines reusable settings.*default_settingsinserts them underserver.
7. Merging Data (<<)
Instead of manually copying values, YAML allows merging dictionaries:
base: &base
os: Linux
version: "1.0"
app:
<<: *base
name: MyApp
8. YAML vs JSON
| Feature | YAML | JSON |
|---|---|---|
| Syntax | Uses indentation | Uses {} and [] |
| Readability | More human-friendly | More compact |
| Comments | # This is a comment |
β Not supported |
| Data Types | Strings, Numbers, Lists, Dictionaries | Same |
| Multiline Strings | Yes (|and>) |
|
| Parsing | Slower | Faster |
β
JSON is better for APIs
β
YAML is better for configuration files
9. YAML in Different Tools
Kubernetes Example (deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
Docker Compose Example (docker-compose.yml)
version: "3.8"
services:
web:
image: nginx
ports:
- "80:80"
10. Parsing YAML in Code
Python (PyYAML)
import yaml
data = yaml.safe_load("""
name: Alice
age: 30
hobbies:
- Reading
- Gaming
""")
print(data["name"]) # Alice
PowerShell
$yaml = Get-Content config.yaml | ConvertFrom-Yaml
$yaml.name # Output: Alice
11. Comments in YAML
YAML supports inline comments using #:
name: Alice # This is a name
π¨ No multi-line comments in YAML.
12. Best Practices for YAML
β
Use spaces, not tabs
β
Use - for lists and indentation for structure
β
Use | for multiline strings if formatting matters
β
Use # for comments
β
Use anchors (&) and aliases (*) for reusable data
13. Common Uses of YAML
- Configuration files (Kubernetes, Docker Compose, GitHub Actions)
- Data serialization (Ansible, CloudFormation)
- API Definitions (OpenAPI/Swagger)
- CI/CD Pipelines (GitLab, GitHub Actions)
Key Takeaways
β
YAML is indentation-based, no brackets or commas.
β
Used for config files, automation, and DevOps tools.
β
Supports anchors, aliases, and merging for reusable data.
β
More readable than JSON, but parses slower.
Need deeper insights into a specific YAML feature? π