正在加载,请稍候…

YAML Viewer and Formatter: Validate and Pretty-Print YAML

Format and validate YAML files online. Learn YAML syntax, indentation rules, common errors, and best practices for Kubernetes, Docker Compose, and GitHub Actions configs.

What Is YAML?

YAML (YAML Ain't Markup Language, a recursive acronym) is a human-friendly data serialization standard. Designed to be readable by both humans and machines, YAML uses indentation and minimal punctuation to express data structures.

YAML has become the dominant format for configuration files in modern infrastructure and DevOps: Docker Compose, Kubernetes manifests, GitHub Actions workflows, Ansible playbooks, and many application configuration frameworks all use YAML.

YAML Data Types

YAML automatically infers data types based on the value:

# Strings (no quotes needed unless value is ambiguous)
name: Alice
message: "This requires quotes: it has a colon"
long_string: 'Single quotes also work'

# Integers and floats
port: 8080
price: 19.99
negative: -5

# Booleans (careful - many values are truthy)
enabled: true     # or yes, on, True, TRUE
disabled: false   # or no, off, False, FALSE

# Null
optional: null    # or ~
empty:            # Also null

# Dates
birthday: 1990-01-15
created_at: 2025-06-15T14:30:00Z

YAML Collections

Mappings (Objects)

user:
  id: 1
  name: Alice
  email: alice@example.com
  address:
    city: New York
    country: US

# Flow style (compact, like JSON)
user: {id: 1, name: Alice, email: alice@example.com}

Sequences (Arrays)

# Block style
tags:
  - python
  - javascript
  - docker

# Flow style
tags: [python, javascript, docker]

# Array of objects
users:
  - id: 1
    name: Alice
  - id: 2
    name: Bob

YAML Anchors and Aliases

YAML's most powerful feature for avoiding repetition:

# Define an anchor with &
default_config: &defaults
  database: postgres
  pool_size: 10
  timeout: 30
  retry: 3

# Reuse with *
production:
  <<: *defaults          # Merge all defaults
  pool_size: 50          # Override specific value
  environment: prod

staging:
  <<: *defaults
  environment: staging

Multi-Line Strings

YAML handles multi-line strings elegantly:

# Literal block (|) - preserves newlines and trailing newline
script: |
  #!/bin/bash
  set -e
  echo "Starting deployment"
  npm run build
  pm2 restart app

# Folded block (>) - joins lines with spaces
description: >
  This is a long description that
  will be joined into a single
  paragraph.

# Chomping indicators
text_trim: |-     # No trailing newline
  content
text_keep: |+     # Keep all trailing newlines
  content

YAML Best Practices

Use Consistent Indentation

YAML is indentation-sensitive. Choose 2 or 4 spaces and stick to it throughout the file. Never mix tabs.

Quote Strings That Look Like Other Types

# Without quotes, these become non-strings
version: "1.0"        # String, not float 1.0
zip: "01234"          # String, not int 1234 (loses leading zero)
active: "yes"         # String, not boolean true

Use Descriptive Comments

# Database connection settings
database:
  host: localhost      # Use 'db' in Docker Compose
  port: 5432           # Default PostgreSQL port
  name: myapp

Common YAML Use Cases

Kubernetes: Every resource definition (Pod, Deployment, Service, ConfigMap) is a YAML file.

GitHub Actions: Workflow files defining CI/CD pipelines.

Docker Compose: Multi-container application definitions.

Ansible: Infrastructure automation playbooks and inventory files.

Spring Boot: Application configuration (application.yml).

Ruby on Rails: Database configuration (database.yml), environment configs.

YAML Pitfalls

The Norway Problem: In YAML 1.1, the value NO (country code for Norway) is parsed as false. YAML 1.2 fixed this, but many parsers still use YAML 1.1 behavior. When in doubt, quote strings.

Tab prohibition: YAML 1.1 completely prohibits tab characters for indentation. Always use spaces.

Float parsing: Values like 1.0, 1e5, .inf, .nan are parsed as floats/special values in some parsers.

Using This Tool

Paste any YAML document to instantly validate it, view the parsed structure as a tree, and see any syntax errors with line numbers. The tool highlights each data type with different colors for easy identification.

-> Try the YAML Viewer