FormatForge

What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files. It's the standard format for Kubernetes, Docker Compose, Ansible, and many CI/CD pipelines.

YAML Syntax Basics

YAML uses indentation to represent structure. Here are the key concepts:

  • Key-value pairs use colons: key: value
  • Indentation creates hierarchy (use spaces, not tabs)
  • Lists use dashes: - item
  • Comments start with #
  • Strings usually don't need quotes

Basic YAML Example

# User configuration
name: John Doe
age: 30
email: john@example.com
active: true

address:
  city: New York
  country: USA
  zipcode: "10001"  # Quoted to preserve string

hobbies:
  - reading
  - gaming
  - hiking

YAML Data Types

Strings

# Plain strings (no quotes needed usually)
name: John Doe

# Quoted strings (for special characters)
message: "Hello: World"
path: 'C:\Users\john'

# Multiline strings
description: |
  This is a multiline string.
  Line breaks are preserved.

folded: >
  This is a folded string.
  Line breaks become spaces.

Numbers and Booleans

# Numbers
integer: 42
float: 3.14
scientific: 1.0e+12
hex: 0xFF

# Booleans (many formats accepted)
enabled: true
disabled: false
yes_value: yes
no_value: no

Lists and Objects

# List (block style)
fruits:
  - apple
  - banana
  - orange

# List (flow style - like JSON)
colors: [red, green, blue]

# Nested objects
database:
  host: localhost
  port: 5432
  credentials:
    username: admin
    password: secret

Common YAML Use Cases

1. Kubernetes Manifests

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    spec:
      containers:
        - name: nginx
          image: nginx:1.21
          ports:
            - containerPort: 80

2. Docker Compose

version: '3.8'
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db

  db:
    image: postgres:14
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

3. GitHub Actions

name: CI Pipeline
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm test

Common YAML Mistakes

Using tabs instead of spaces

YAML requires spaces for indentation. Tabs cause parsing errors.

Inconsistent indentation

Use the same number of spaces throughout (usually 2).

Forgetting to quote special strings

time: 12:30 # Interpreted as minutes!time: "12:30" # Correct

Missing space after colon

key:value # Wrongkey: value # Correct

YAML vs JSON vs TOML

FeatureYAMLJSONTOML
CommentsYesNoYes
Human ReadableExcellentGoodGood
Indentation-basedYesNoNo
Primary UseConfig filesAPIs, dataConfig files

Learn more about other formats:

Frequently Asked Questions

What does YAML stand for?

YAML stands for 'YAML Ain't Markup Language' (a recursive acronym). Originally it stood for 'Yet Another Markup Language', but was changed to emphasize that YAML is for data, not documents.

Why is YAML popular for Kubernetes?

YAML is popular for Kubernetes because it's human-readable, supports comments for documentation, and can represent complex nested configurations clearly. It's also less verbose than JSON or XML.

Is YAML better than JSON?

YAML is often better for configuration files because it supports comments and is more readable. JSON is better for APIs and data exchange because it's faster to parse and has stricter syntax.

Why does indentation matter in YAML?

YAML uses indentation to define structure and hierarchy (like Python). Unlike JSON which uses braces, YAML relies on consistent spacing to determine parent-child relationships.

YAML Tools