YAML Introduction

What is YAML?

Understand the human-friendly data serialization standard. Learn its syntax, usage, and why it has become the preferred format for configuration files.

Definition

YAML (YAML Ain't Markup Language) is a human-readable data serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML is designed to be easy to read and write for humans, while also being easy for machines to parse and generate.

What is YAML Used For?

YAML is widely used in various scenarios, especially where human-readable configuration is needed.

Configuration Files

The preferred format for Docker Compose, Kubernetes, Ansible, and other tools.

CI/CD Pipelines

GitHub Actions, GitLab CI, CircleCI use YAML to define workflows.

Data Serialization

Transfer complex data structures between applications with support for references and anchors.

Core Features

Why has YAML become the preferred format for configuration files?

Human Readable

Uses indentation and clean syntax, easier to read and write than JSON and XML.

Comment Support

Can add comments to explain configuration, a feature JSON lacks.

Rich Data Types

Supports strings, numbers, booleans, arrays, objects, dates, and more.

Reference Support

Enables data reuse through anchors (&) and aliases (*), avoiding duplication.

YAML Syntax Rules

YAML uses indentation to represent hierarchy, supporting key-value pairs, lists, and multi-line strings.

1

Indentation shows hierarchy

Use spaces (not tabs) for indentation, typically 2 spaces. Indentation represents data nesting.

2

Key-value pairs use colons

Keys and values are separated by a colon and space (key: value).

3

Lists use dashes

List items start with a dash (-) followed by a space.

4

Multi-line string support

Use

# YAML Example
name: John Doe
age: 30
active: true
skills:
  - JavaScript
  - Python
  - YAML
address:
  city: New York
  country: USA
bio: |
  Multi-line string
  with preserved newlines

YAML vs JSON

Comparing these two popular data formats.

FeatureYAMLJSON
ReadabilityVery easy (clean syntax)Easier (with brackets)
Comment SupportSupported (# comments)Not supported
Syntax ComplexityMore complex (indentation-sensitive)Simple (explicit brackets)
Data TypesRich (dates, references, etc.)Basic (6 types)
File SizeSmaller (no redundant symbols)Larger (brackets and quotes)

Best Practices

How to use YAML effectively and avoid common pitfalls.

Use Spaces, Not Tabs

YAML specification requires spaces for indentation, not tabs. Recommended: 2 spaces.

Use Quotes Carefully

Most strings don't need quotes, but use single or double quotes when containing special characters.

Leverage Anchors and Aliases

Use & to define anchors, * to reference aliases, avoiding duplicate configuration.

Add Version Declaration

Use %YAML 1.2 at the beginning of the file to declare YAML version for parser compatibility.

Frequently Asked Questions

Which is better, YAML or JSON?

It depends on the use case. YAML is better for configuration files because it supports comments and is more readable. JSON is better for API data transfer because it parses faster and has simpler syntax.

Must YAML indentation be 2 spaces?

No. YAML allows any number of spaces for indentation, but the same level must be consistent. 2 spaces is the most common convention.

Is YAML safe?

YAML parsers can have security risks, especially when processing untrusted input. It's recommended to use safe-mode parsers to avoid executing arbitrary code.