What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization language commonly used for configuration files and data exchange. It uses indentation to represent structure, making it more readable than JSON or XML for complex nested data.

Quick Facts

Full NameYAML Ain't Markup Language
Created2001 by Clark Evans, Ingy döt Net, Oren Ben-Kiki
SpecificationOfficial Specification

How It Works

YAML uses whitespace indentation to denote structure, eliminating the need for brackets or tags. It supports multiple data types including strings, numbers, booleans, null, arrays (sequences), and objects (mappings). YAML allows comments (starting with #), multi-line strings, and anchors/aliases for reusing content. It's a superset of JSON, meaning valid JSON is also valid YAML. YAML is widely used in DevOps tools like Docker Compose, Kubernetes, Ansible, and CI/CD pipelines. Security considerations: YAML parsers in some languages (notably Python's PyYAML with yaml.load()) can execute arbitrary code through special tags. Always use safe loading functions (yaml.safe_load()) when parsing untrusted YAML. The !!python/object tag and similar constructs should be avoided in user-facing configurations.

Key Characteristics

  • Uses indentation for structure (spaces only, no tabs)
  • Supports comments with # symbol
  • Multi-line string support with | and > operators
  • Anchors (&) and aliases (*) for content reuse
  • Superset of JSON - all JSON is valid YAML
  • Multiple documents in one file separated by ---

Common Use Cases

  1. Configuration files (Docker Compose, Kubernetes)
  2. CI/CD pipeline definitions (GitHub Actions, GitLab CI)
  3. Infrastructure as Code (Ansible, CloudFormation)
  4. API specifications (OpenAPI/Swagger)
  5. Static site generators (Jekyll, Hugo)

Example

loading...
Loading code...

Frequently Asked Questions

What is the difference between YAML and JSON?

YAML uses indentation for structure and supports comments, while JSON uses braces and brackets without comments. YAML is more human-readable but whitespace-sensitive. YAML is a superset of JSON, meaning valid JSON is also valid YAML.

Can I use tabs for indentation in YAML?

No, YAML only allows spaces for indentation. Using tabs will cause parsing errors. This strict rule ensures consistent formatting across different editors and systems. Most editors can be configured to insert spaces when pressing Tab.

What is the difference between | and > for multi-line strings in YAML?

The pipe (|) preserves line breaks as literal newlines, keeping the text exactly as written. The greater-than (>) folds lines into a single line, replacing newlines with spaces. Both support modifiers like |- to strip trailing newlines.

Are there security concerns with YAML parsing?

Yes, some YAML parsers can execute arbitrary code through special tags like !!python/object. Always use safe loading functions (yaml.safe_load() in Python) when parsing untrusted YAML. Avoid custom type tags in user-facing configurations.

What are YAML anchors and aliases?

Anchors (&name) mark a node for reuse, and aliases (*name) reference that node elsewhere. This avoids duplication in configuration files. For example, &defaults can define common settings that multiple sections reference with *defaults.

Related Tools

Related Terms

Related Articles