What is YAML? — Human-Readable Config Format Explained
Definition
YAML (YAML Ain't Markup Language, a recursive acronym) is a human-readable data serialization format designed for configuration files and data exchange. Unlike JSON, YAML uses indentation to express structure rather than braces and brackets, making it more concise and easier to read for complex, nested configurations. It is the standard format for Kubernetes manifests, Docker Compose files, GitHub Actions workflows, Ansible playbooks, and many other DevOps tools.
How It Works
YAML structures are expressed through indentation (typically 2 spaces). A mapping (equivalent to a JSON object) is written as key: value pairs, one per line. A sequence (array) uses - as a prefix. Strings generally do not need quotes unless they contain special characters like :, #, or leading spaces. YAML supports comments with #, multi-line strings (block scalars with | or >), and a powerful reuse mechanism called anchors (&) and aliases (*) that lets you reference the same value in multiple places.
Common Use Cases
- ▸Writing Kubernetes deployment and service manifests
- ▸Defining GitHub Actions CI/CD workflow steps
- ▸Configuring Docker Compose multi-container applications
- ▸Writing Ansible playbooks and roles
- ▸Structuring complex configuration files in Ruby on Rails, Jekyll, and other frameworks
Example
server:
host: localhost
port: 8080
tls: true
services:
- name: api
image: my-api:latest
replicas: 3
- name: worker
image: my-worker:latest
replicas: 1
# Comment: this is valid YAMLRelated Tools
Format and beautify YAML with consistent indentation (2 or 4 spaces).
Validate YAML syntax and preview the parsed JSON representation.
Convert YAML to JSON with proper structure and types.
Convert JSON to YAML format with proper indentation.
FAQ
- What are the most common YAML mistakes?
- The most frequent YAML errors are: incorrect indentation (YAML is whitespace-sensitive), using tabs instead of spaces (tabs are not allowed), forgetting to quote strings that start with special characters like : or -, and the "Norway problem" where country code "NO" is parsed as boolean false.
- What is the Norway problem in YAML?
- In YAML 1.1, certain strings like yes, no, on, off, true, false are parsed as booleans. So a country code list that includes "NO" would have it parsed as false. YAML 1.2 (used by most modern parsers) only recognizes true and false as booleans, fixing this issue.
- Is JSON valid YAML?
- Yes. YAML 1.2 is a superset of JSON, meaning any valid JSON document is also valid YAML. However, mixing JSON syntax into YAML files is rarely useful and reduces readability.