Advertisement
Back to Format List

YAML Format

YAML Ain't Markup Language - Human-readable data serialization standard

What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization standard. It is designed to be human-friendly and work well with modern programming languages. YAML uses indentation to represent structure, avoiding extensive use of brackets and quotes.

YAML is commonly used for configuration files, data exchange, document markup, and other scenarios. Its syntax is concise and clear, supports comments, and is very suitable for configuration files that need to be manually edited and maintained. Many modern tools and frameworks use YAML as their configuration format.

Syntax Structure
Basic syntax rules and data types for YAML

Basic Rules:

  • Use indentation to represent hierarchy (usually 2 spaces)
  • Cannot use tabs, only spaces
  • Key-value pairs separated by colons, must have space after colon
  • Array items start with dash followed by space
  • Supports comments (starting with #)
  • Case sensitive

Data Types:

Scalar - strings, numbers, booleans
Sequence - arrays/lists
Mapping - objects/dictionaries
null - null values
Multiline strings - text blocks
References - anchors and aliases
Examples
Common YAML data structure examples

Basic Configuration:

# Application Configuration
app:
  name: "My App"
  version: "1.0.0"
  debug: true
  
database:
  host: "localhost"
  port: 5432
  username: "admin"
  password: "secret"
  
# Environment Configuration
environments:
  - development
  - testing
  - production

User Data:

users:
  - id: 1
    name: "John Doe"
    email: "john.doe@example.com"
    active: true
    roles:
      - admin
      - user
    profile:
      age: 28
      city: "New York"
      
  - id: 2
    name: "Jane Smith"
    email: "jane.smith@example.com"
    active: false
    roles:
      - user
    profile:
      age: 32
      city: "London"

Multiline Text and References:

# Anchor Definition
defaults: &defaults
  timeout: 30
  retries: 3

# Multiline Text
description: |
  This is a multiline
  description that preserves
  newlines.

summary: >
  This is a folded
  multiline text that will be
  combined into a single line.

# Using References
api:
  endpoint1:
    <<: *defaults
    url: "/api/v1/users"
  
  endpoint2:
    <<: *defaults
    url: "/api/v1/orders"
    timeout: 60  # Override default value
Advantages
  • Extremely human-readable
  • Supports comments
  • Clear hierarchical structure
  • Supports complex data types
  • Preferred format for configuration files
Disadvantages
  • Sensitive to indentation, error-prone
  • Relatively complex parsing
  • Not suitable for large datasets
  • Relatively steep learning curve
  • Complex behavior in some edge cases
Common Use Cases

Configuration Files:

  • Docker Compose configuration
  • Kubernetes resource definitions
  • CI/CD pipeline configuration
  • Application configuration

Development Tools:

  • Ansible playbooks
  • Swagger API documentation
  • Static site generator configuration
  • Package manager configuration

Data Exchange:

  • Configuration data import/export
  • Structured document writing
  • Template system configuration
  • Multi-language localization files

Infrastructure:

  • Cloud service configuration
  • Monitoring system configuration
  • Log collection configuration
  • Service mesh configuration
Best Practices

Writing Standards:

  • Consistently use 2-space indentation
  • Add space after colon in key-value pairs
  • Use comments appropriately to explain complex configurations
  • Use references (anchors) to avoid duplication

Structure Design:

  • Keep hierarchical structure simple and clear
  • Use meaningful key names
  • Avoid overly deep nesting levels
  • Group related configuration items appropriately

Maintenance Tips:

  • Use YAML validation tools to check syntax
  • Version control configuration file changes
  • Establish a review process for production environment configurations
  • Regularly back up important configuration files