Guide to YAML Format: Syntax, Examples & Best Practices
YAML (YAML Ain't Markup Language) has become the de facto standard for configuration files in modern software development. From Docker Compose to Kubernetes manifests, GitHub Actions to Ansible playbooks, YAML is everywhere. Its human-readable syntax makes it more approachable than JSON or XML for configuration, but its whitespace-sensitive nature can trip up newcomers. This guide covers everything you need to know to read, write, and debug YAML confidently.
What Is YAML?
YAML is a data serialization language designed to be human-friendly. It was first proposed in 2001 and has gone through several revisions, with YAML 1.2 being the current standard. Unlike JSON, YAML was specifically designed for configuration files and data exchange where humans need to read and edit the content regularly.
Key characteristics of YAML:
- Uses indentation (spaces, never tabs) to represent structure
- No curly braces, brackets, or quotes required for most values
- Supports comments (JSON doesn't)
- Is a superset of JSON — any valid JSON is also valid YAML
Basic Syntax
Key-Value Pairs
The simplest YAML structure is a key-value pair, separated by a colon and a space:
name: John Doe age: 30 email: john@example.com active: true
Nested Objects
Use indentation (typically 2 spaces) to create nested structures:
person:
name: John Doe
address:
street: 123 Main St
city: New York
country: US
Lists (Arrays)
Use a dash followed by a space for list items:
fruits: - apple - banana - cherry # Inline list (flow style) colors: [red, green, blue]
Lists of Objects
employees:
- name: Alice
role: Developer
skills:
- Python
- JavaScript
- name: Bob
role: Designer
skills:
- Figma
- CSS
Data Types
YAML automatically infers data types, which is both a feature and a common source of bugs:
# Strings name: John Doe # unquoted string title: "Manager" # double-quoted (allows escape sequences) note: 'It''s fine' # single-quoted (literal, escape ' with '') # Numbers integer: 42 float: 3.14 hex: 0xFF octal: 0o77 scientific: 1.0e+5 # Booleans active: true deleted: false # WARNING: yes, no, on, off are also booleans in YAML 1.1! # enabled: yes → this is boolean true, not the string "yes" # Null value: null also_null: ~ empty_value: # Dates date: 2026-02-14 datetime: 2026-02-14T10:30:00Z
The Norway Problem
One of YAML's most infamous gotchas: country codes like NO (Norway) are interpreted as boolean false in YAML 1.1. The fix is to always quote strings that could be misinterpreted:
countries: - US - UK - "NO" # Must quote! Otherwise YAML reads it as false - DE
Multi-Line Strings
YAML offers two powerful ways to handle multi-line text:
Literal Block Scalar (|)
Preserves newlines exactly as written:
description: | This is line one. This is line two. This is after a blank line.
Folded Block Scalar (>)
Folds newlines into spaces (like a word-wrapped paragraph):
description: > This is a very long description that will be folded into a single line with spaces.
Anchors and Aliases
YAML supports anchors (&) and aliases (*) to avoid repetition — a feature JSON completely lacks:
defaults: &defaults adapter: postgres host: localhost port: 5432 development: database: myapp_dev <<: *defaults production: database: myapp_prod <<: *defaults host: db.production.com
The << merge key combined with an alias pulls in all key-value pairs from the anchor, which can then be overridden individually.
Real-World YAML Examples
Docker Compose
version: "3.8"
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- api
api:
build: ./api
environment:
- DATABASE_URL=postgres://localhost/myapp
restart: unless-stopped
GitHub Actions
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
YAML vs JSON vs XML
Each format has its strengths:
- YAML: Best for configuration files humans edit frequently. Supports comments, anchors, multi-line strings. Whitespace-sensitive.
- JSON: Best for data interchange between systems. Simpler parsing, no ambiguity. No comments. Learn more in our JSON format guide.
- XML: Best for document-centric data with mixed content. Verbose but self-describing. See our XML vs JSON comparison.
Need to convert between formats? Use our YAML to JSON Converter to transform YAML to JSON and back instantly.
Common YAML Pitfalls
- Tabs vs spaces: YAML only allows spaces for indentation. A single tab will cause a parse error.
- Inconsistent indentation: Pick 2 spaces and stick with it throughout the file.
- Unquoted special values:
yes,no,true,false,null,~, and numeric strings like1.0can be misinterpreted. - Trailing spaces: Can cause subtle parsing issues that are hard to spot.
- Colon in values: If your value contains a colon followed by a space, you must quote it:
message: "Error: something failed"
Validating YAML
Always validate your YAML files before deploying. A misplaced space can bring down your entire infrastructure. Use our YAML to JSON Converter as a quick validator — if it converts successfully, your YAML is valid. For more complex validation, tools like yamllint can enforce style rules and catch common mistakes.
Conclusion
YAML is powerful and readable when used correctly, but its implicit type coercion and whitespace sensitivity require care. Remember: use spaces not tabs, quote ambiguous values, and validate before deploying. Our YAML to JSON Converter and JSON Formatter are handy companions for working with configuration files daily.
YAML to JSON Converter · JSON Formatter · XML to JSON Converter · SQL Formatter