Understand the human-friendly data serialization standard. Learn its syntax, usage, and why it has become the preferred format for configuration files.
YAML (YAML Ain't Markup Language) is a human-readable data serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML is designed to be easy to read and write for humans, while also being easy for machines to parse and generate.
YAML is widely used in various scenarios, especially where human-readable configuration is needed.
The preferred format for Docker Compose, Kubernetes, Ansible, and other tools.
GitHub Actions, GitLab CI, CircleCI use YAML to define workflows.
Transfer complex data structures between applications with support for references and anchors.
Why has YAML become the preferred format for configuration files?
Uses indentation and clean syntax, easier to read and write than JSON and XML.
Can add comments to explain configuration, a feature JSON lacks.
Supports strings, numbers, booleans, arrays, objects, dates, and more.
Enables data reuse through anchors (&) and aliases (*), avoiding duplication.
YAML uses indentation to represent hierarchy, supporting key-value pairs, lists, and multi-line strings.
Use spaces (not tabs) for indentation, typically 2 spaces. Indentation represents data nesting.
Keys and values are separated by a colon and space (key: value).
List items start with a dash (-) followed by a space.
Use
# YAML Example
name: John Doe
age: 30
active: true
skills:
- JavaScript
- Python
- YAML
address:
city: New York
country: USA
bio: |
Multi-line string
with preserved newlinesComparing these two popular data formats.
| Feature | YAML | JSON |
|---|---|---|
| Readability | Very easy (clean syntax) | Easier (with brackets) |
| Comment Support | Supported (# comments) | Not supported |
| Syntax Complexity | More complex (indentation-sensitive) | Simple (explicit brackets) |
| Data Types | Rich (dates, references, etc.) | Basic (6 types) |
| File Size | Smaller (no redundant symbols) | Larger (brackets and quotes) |
How to use YAML effectively and avoid common pitfalls.
YAML specification requires spaces for indentation, not tabs. Recommended: 2 spaces.
Most strings don't need quotes, but use single or double quotes when containing special characters.
Use & to define anchors, * to reference aliases, avoiding duplicate configuration.
Use %YAML 1.2 at the beginning of the file to declare YAML version for parser compatibility.
It depends on the use case. YAML is better for configuration files because it supports comments and is more readable. JSON is better for API data transfer because it parses faster and has simpler syntax.
No. YAML allows any number of spaces for indentation, but the same level must be consistent. 2 spaces is the most common convention.
YAML parsers can have security risks, especially when processing untrusted input. It's recommended to use safe-mode parsers to avoid executing arbitrary code.