Skip to Content
Go Realm v1 is released 🎉
DevOpsYAML

YAML (YAML Ain’t Markup Language)

What is YAML?

YAML (YAML Ain’t Markup Language) is a human-readable data serialization format, commonly used for

  • configuration files and
  • data exchange.

Basic Syntax

Key-Value Pairs

key: value name: John Doe age: 30 is_student: false

Strings

# Single line name: John Doe # Multi-line (preserves newlines) description: | This is a multi-line string # Multi-line (folds newlines) # same as # description: "This will be a single line string" description: > This will be a single line string

Numbers

integer: 42 float: 3.14 scientific: 1.2e+34 infinity: .inf not_a_number: .nan

Booleans

true_value: true false_value: false yes: yes # Also true no: no # Also false

Null Values

null_value: null empty: ~ # Also null

Collections

Lists/Arrays

# Block style fruits: - Apple - Banana - Orange # Inline style fruits: [Apple, Banana, Orange]

Dictionaries/Maps

# Block style person: name: John age: 30 city: New York # Inline style person: {name: John, age: 30, city: New York}

Nested Structures

users: - name: John age: 30 hobbies: - reading - swimming - name: Jane age: 25 hobbies: [hiking, photography]

Advanced Features

Anchors and Aliases (Reuse)

defaults: &defaults adapter: postgres host: localhost development: <<: *defaults database: dev_db test: <<: *defaults database: test_db

Multi-document Files

--- # Document 1 name: John age: 30 --- # Document 2 name: Jane age: 25

Comments

# This is a comment key: value # Inline comment

Special Syntax

Environment Variables

# Some YAML parsers support this database_url: ${DATABASE_URL:-sqlite:///default.db}

Type Casting (YAML 1.2)

# Some parsers support explicit types string: !!str 123 integer: !!int "123" float: !!float "3.14" boolean: !!bool "true"

Common Use Cases

Docker Compose YAML and JSON

YAML Format

version: '3.8' services: web: image: nginx:alpine ports: - "80:80" environment: - NODE_ENV=production volumes: - ./app:/app database: image: postgres:13 environment: POSTGRES_DB: myapp POSTGRES_USER: user POSTGRES_PASSWORD: pass

JSON Format

{ "version": "3.8", "services": { "web": { "image": "nginx:alpine", "ports": [ "80:80" ], "environment": [ "NODE_ENV=production" ], "volumes": [ "./app:/app" ] }, "database": { "image": "postgres:13", "environment": { "POSTGRES_DB": "myapp", "POSTGRES_USER": "user", "POSTGRES_PASSWORD": "pass" } } } }

CI/CD Pipeline YAML and JSON

YAML Format

name: CI/CD Pipeline on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Run tests run: npm test

JSON Format

{ "name": "CI/CD Pipeline", "on": ["push", "pull_request"], "jobs": { "test": { "runs-on": "ubuntu-latest", "steps": [ { "uses": "actions/checkout@v2" }, { "name": "Run tests", "run": "npm test" } ] } } }

Best Practices

1. Indentation

# Good (2 spaces) key: nested: - item1 - item2 # Bad (mixing tabs and spaces) key: nested: - item1 - item2

2. Quotes

# Use quotes when needed special_chars: "value with: colon" boolean_string: "true" numeric_string: "123"

3. Readability

# Good - clear structure server: host: localhost port: 8080 ssl: enabled: true cert: /path/to/cert # Bad - too compact server: {host: localhost, port: 8080, ssl: {enabled: true, cert: /path/to/cert}}

Common Pitfalls

1. Boolean Interpretation

# These might be interpreted as booleans yes: yes # → true no: no # → false on: on # → true off: off # → false # Use quotes to force string yes: "yes" # → "yes"

2. Floating Point Numbers

# This might be interpreted as time version: 3.10 # → 3.1 (float) version: "3.10" # → "3.10" (string)

3. Special Characters

# Use quotes for special characters special: "value with: colon & asterisk *"

Validation Tools

Online Validators

Command Line

# Install yamllint pip install yamllint # Validate file yamllint file.yaml # Validate with specific rules yamllint -d relaxed file.yaml

YAML vs JSON

FeatureYAMLJSON
ReadabilityHighMedium
CommentsYesNo
Multi-line stringsYesNo
References/AnchorsYesNo
Strict syntaxLess strictVery strict
File sizeSmallerLarger
# YAML person: name: John age: 30 hobbies: [reading, swimming]
// JSON { "person": { "name": "John", "age": 30, "hobbies": ["reading", "swimming"] } }

Remember: YAML is a superset of JSON - all valid JSON is valid YAML!

Key Features of YAML:

  • Human-readable: YAML is designed to be easy to read and write.
  • Hierarchical structure: Data is represented in a tree structure, which makes it easy to represent complex data.
  • Indentation-based: YAML relies on indentation to represent the structure, so there are no brackets or other delimiters.
  • Supports complex data types: YAML supports scalar values (strings, numbers), lists, and maps (key-value pairs).