What is YAML? — YAML Ain't Markup Language Explained
Definition
YAML (YAML Ain't Markup Language) is a human-readable data serialization standard used for configuration files, data exchange, and structured data storage. Unlike markup languages (like HTML or XML), YAML focuses on data rather than document structure. Its indentation-based syntax makes it cleaner and more readable than JSON or XML, especially for configuration files that humans edit regularly.
YAML Syntax Basics
YAML relies on indentation (spaces, not tabs) to define structure:
# This is a YAML comment
key: value
nested:
child_key: child_value
list:
- item1
- item2
- item3Key Rules
- Use spaces for indentation (typically 2 spaces)
- Do not use tabs
- Indentation level defines nesting depth
- Comments start with
# - Quotes are optional for strings (unlike JSON)
YAML Data Types
| Type | Example | Notes |
|---|---|---|
| String | hello, "hello", 'hello' |
Quotes optional |
| Integer | 42, -7, 0xFF |
Decimal, hex, octal |
| Float | 3.14, 1.0e+6, inf |
Scientific notation supported |
| Boolean | true, false, yes, no |
Multiple representations |
| Null | null, ~, (empty) |
Various null representations |
| Date | 2024-01-15 |
ISO 8601 format |
| Timestamp | 2024-01-15T14:30:00Z |
ISO 8601 with time |
| Sequence | [1, 2, 3] or block list |
Ordered list |
| Mapping | {key: value} or block mapping |
Key-value pairs |
Indentation Rules
Indentation is YAML's most important — and most error-prone — feature:
# Two spaces per level
person:
name: Alice
address:
city: New York
zip: "10001"
hobbies:
- reading
- hikingRules:
- Same-level items must have the same indentation
- Children are indented deeper than their parent
- Tabs are not allowed as indentation
- Consistent indentation is critical
Anchors and Aliases
YAML supports anchors (&) and aliases (*) to reuse content:
defaults: &defaults
adapter: postgres
host: localhost
development:
database: myapp_dev
<<: *defaults
production:
database: myapp_prod
<<: *defaults
host: prod-db.example.comThis is equivalent to:
development:
adapter: postgres
host: localhost
database: myapp_dev
production:
adapter: postgres
host: prod-db.example.com
database: myapp_prodMulti-line Strings
YAML offers several ways to handle multi-line strings:
# Literal block (preserves line breaks)
description: |
This is a multi-line
string that preserves
line breaks.
# Folded block (wraps lines)
summary: >
This long text will be
folded into a single
line when parsed.
# Double-quoted (escape sequences)
message: "Hello\nWorld\n"JSON Compatibility
YAML 1.2 is a superset of JSON — any valid JSON file is also a valid YAML file:
{
"name": "Alice",
"age": 30
}This is valid YAML because JSON syntax is a subset of YAML 1.2.
Common Use Cases
DevOps and Infrastructure
- Kubernetes — Pods, Deployments, Services (all YAML)
- Docker Compose — Multi-container definitions
- Ansible — Playbooks and inventory
- CI/CD — GitHub Actions, GitLab CI, CircleCI
Application Configuration
- Ruby on Rails — database.yml, application.yml
- Symfony — PHP framework configs
- Logstash/Filebeat — Data pipeline configs
API Specifications
- OpenAPI (Swagger) — API documentation
- Serverless Framework — Cloud function configs
LangStop YAML Tools
- YAML Formatter — Format and beautify
- YAML Validator — Validate syntax
- YAML to JSON — Convert to JSON
- YAML to XML — Convert to XML
- YAML to TOML — Convert to TOML
- JSON to YAML — Convert JSON to YAML