AskDB
·6 min read

YAML Guide: Syntax and Examples

YAML is a human-readable data serialization format. It is widely used for configuration files in Docker, Kubernetes, GitHub Actions, Ansible, and many other tools.

Basic Syntax

YAML uses indentation (spaces, not tabs) to structure data. Key-value pairs use colons.

name: Alice
age: 30
active: true

Data Types

string: hello world
quoted: "value with special chars: yes"
number: 42
float: 3.14
boolean: true
null_value: null
date: 2024-01-15

Lists (Arrays)

fruits:
  - apple
  - banana
  - cherry

# Inline format
colors: [red, green, blue]

Nested Objects

person:
  name: Alice
  address:
    city: New York
    country: USA
  hobbies:
    - reading
    - coding

Multi-line Strings

# Literal block (preserves newlines)
description: |
  First line
  Second line
  Third line

# Folded block (converts newlines to spaces)
summary: >
  This is a long sentence
  that spans multiple lines
  but becomes one line.

Anchors and Aliases (Reuse)

defaults: &defaults
  timeout: 30
  retries: 3

development:
  <<: *defaults
  host: localhost

production:
  <<: *defaults
  host: prod.example.com

Comments

# This is a comment
name: value  # Inline comment

Common Use Cases

  • Docker Compose - defining multi-container applications
  • Kubernetes - pod, service, and deployment configs
  • GitHub Actions - CI/CD workflow definitions
  • Ansible - infrastructure automation playbooks
  • Config files - application settings

YAML vs JSON

  • YAML supports comments, JSON does not
  • YAML uses indentation, JSON uses braces
  • YAML is more human-readable, JSON is more machine-readable
  • YAML can reference other parts of the document (anchors)

Convert YAML to JSON

Need to convert YAML to JSON? Use the YAML to JSON converter to instantly convert your YAML configuration to JSON format.

Common Mistakes

  • Using tabs instead of spaces (causes parse errors)
  • Mixing tabs and spaces
  • Forgetting colons after keys
  • Not quoting strings that contain special characters (:, { , } , [, ])