What Is YAML? The .yml File Format Explained

Short answer: YAML is a plain-text format for configuration and data. It uses indentation instead of brackets, so it’s easier to read than JSON. A .yml file and a .yaml file are the same thing: two extensions for one format.

You’ll meet YAML in GitHub Actions workflows, Docker Compose files and Kubernetes manifests. Here’s the syntax, how it compares to JSON, and the gotchas that silently turn your values into something else.

YAML syntax basics

A YAML file is built from three things: maps (key: value pairs), lists (items starting with - ) and scalars (strings, numbers, booleans, null). Nesting is done with indentation, and comments start with #.

Indent with spaces only. The YAML spec forbids tabs in indentation, and parsers reject them. Two spaces per level is the common choice, but any consistent number works.

# A comment
name: my-app
port: 8080
debug: false
tags:
  - web
  - api
database:
  host: localhost
  user: admin
# Flow style, JSON-like, on one line
servers: [web1, web2]
retry: {count: 3, verbose: true}

Loaded into a program, that file becomes ordinary data: name is a string, port a number, debug a boolean, tags a list and database a nested object. The square-bracket and curly-brace lines are flow style, which is handy for short lists.

A tab in the indentation fails immediately. PyYAML reports found character '\t' that cannot start any token. If your editor inserts tabs, switch it to spaces for .yml files.

Multi-line strings: | and >

The pipe | keeps line breaks exactly as written (literal style). The greater-than sign > folds lines into one, joining them with spaces (folded style). Both keep one trailing newline by default; add - (|- or >-) to strip it.

literal: |
  Line one
  Line two
# -> "Line one\nLine two\n"

folded: >
  This long sentence
  becomes one line.
# -> "This long sentence becomes one line.\n"

strip: |-
  no trailing newline
# -> "no trailing newline"

Use | for shell scripts in a CI run: step, where each line is a separate command. Use > for long prose you want to wrap in the file.

Anchors and aliases: reuse a block

An anchor (&name) labels a node, and an alias (*name) reuses it. The merge key << copies an anchored map into another map, where you can override single keys. Docker Compose files use this to share settings between services.

defaults: &defaults
  image: node:22
  restart: always

web:
  <<: *defaults
  ports:
    - "8080:80"

worker:
  <<: *defaults
  restart: "no"   # overrides the default

worker ends up with image: node:22 and restart: "no". Note that << is a YAML 1.1 feature: most parsers support it, but it isn’t part of the 1.2 spec, so check your tool before relying on it.

The gotchas: unquoted values that change type

YAML guesses the type of every unquoted value. Which guess it makes depends on the YAML version your parser follows. YAML 1.1 (2005) treats a long list of words as booleans and reads some numbers in odd bases. YAML 1.2 (2009, latest revision 1.2.2 from 2021) removed most of that. Here’s the same file loaded by PyYAML 6, which follows YAML 1.1, and by js-yaml 4 and the yaml npm package, which follow 1.2:

Value YAML 1.1 (PyYAML) YAML 1.2 (js-yaml, yaml)
country: NO false "NO"
answer: yes true "yes"
enabled: on true "on"
zip: 01234 668 (octal) 1234
time: 22:30 1350 (base 60) "22:30"
version: 1.10 1.1 1.1
empty: null null

The first row is the famous Norway problem: a list of country codes where Norway’s NO becomes false. Notice that even YAML 1.2 isn’t safe for every row: the zip code loses its leading zero and the version number 1.10 becomes the float 1.1.

The fix: quote strings that look like something else

Quoted values are always strings, in every YAML version. Quote country codes, yes/no answers, version numbers, zip codes, phone numbers, times and port mappings. The Docker Compose docs say the same about ports: write HOST:CONTAINER as a quoted string to avoid YAML’s base-60 parsing.

countries: ["GB", "NO", "SE"]
answer: "yes"
zip: "01234"
version: "1.10"
time: "22:30"
ports:
  - "22:22"
  - "8080:80"

Colons and # inside values

A colon followed by a space starts a new key, so title: Error: not found fails with mapping values are not allowed here. Quote the value. A colon without a space after it is fine, which is why url: http://example.com works unquoted.

The same goes for comments: # (space, then hash) starts a comment, so msg: a #b loads as just "a", while a#b stays intact.

title: "Error: not found"   # quoted, works
url: http://example.com      # colon without a space, works
color: "#ff0000"             # unquoted, this would be a comment

YAML vs JSON

YAML 1.2 was designed as a superset of JSON, so most JSON documents are also valid YAML: {"name": "app", "ports": [80, 443]} loads fine in a YAML parser. The differences that matter in practice:

  • Comments: YAML has them, JSON doesn’t. That alone is why most config files are YAML.
  • Readability: YAML drops the braces, brackets and quotes. JSON is noisier but unambiguous.
  • Types: JSON strings are always quoted, so "NO" is never a boolean. YAML guesses, as shown above.
  • Whitespace: in YAML, one wrong indent changes the structure. JSON ignores whitespace.
  • Use: JSON for data between programs (APIs, package.json), YAML for files people edit by hand.

If a JSON value from a form becomes a number somewhere along the way, the same type-guessing problems show up in JavaScript too, as covered in converting a string to a number in JavaScript.

Convert YAML to JSON to check what you wrote

When a config behaves strangely, load it and print it as JSON. You’ll see exactly what the parser understood. This uses PyYAML’s safe_load, which only builds plain data. Never use yaml.load on files you didn’t write: the PyYAML docs warn it can call arbitrary Python functions.

python3 -c 'import sys, json, yaml; print(json.dumps(yaml.safe_load(open(sys.argv[1])), indent=2))' config.yml

Remember this shows PyYAML’s YAML 1.1 view. Your actual tool may use a different parser, but if the JSON shows false where you expected "NO", quoting the value fixes it everywhere.

.yml vs .yaml

There’s no difference in the content. Both extensions are in wide use. Tools usually accept both: GitHub Actions workflow files may use either, and Docker Compose looks for compose.yaml (preferred) or compose.yml, plus the older docker-compose.yaml and docker-compose.yml. Pick one and stay consistent within a project.

Where YAML is used

  • GitHub Actions: workflows in .github/workflows/*.yml.
  • Docker Compose: services, ports and volumes in compose.yaml.
  • Kubernetes: every Deployment, Service and ConfigMap is usually a YAML manifest. The Kubernetes quick tutorial walks through a few.
  • Others: OpenAPI specs, Ansible playbooks, and front matter at the top of Markdown files in static site generators.

One quirk if you process workflow files yourself: PyYAML reads the top-level on: key of a GitHub Actions workflow as the boolean True, because on is a boolean in YAML 1.1. GitHub reads it correctly; your own script may not.

The full rules are in the YAML 1.2.2 specification.

FAQ

What is a .yml file?

A .yml file is a YAML file: plain text that stores configuration or data using indentation, key: value pairs and - list items. You can open and edit it in any text editor.

Is YAML better than JSON?

For files people edit by hand, usually yes, because it’s easier to read and supports comments. For data exchanged between programs, JSON is simpler and has no type guessing.

Can I use tabs in YAML?

Not for indentation. The spec forbids tabs there and parsers throw an error. Use spaces.

What is the Norway problem in YAML?

In YAML 1.1, the unquoted country code NO is parsed as the boolean false. YAML 1.2 parsers read it as a string, but quoting it ("NO") is the safe fix for every parser.

Does YAML care about the number of spaces?

It cares that indentation is consistent within a block, not about the exact number. Two spaces per level is the common convention.