velkaro.Runs in your browser

YAML field notes · 03

YAML anchors, aliases and merge keys: what expands, what overrides, and what JSON gets

&name labels a value, *name repeats it, and <<: *name copies the keys of a mapping into another one. Keys written in the mapping itself always win, in a << list the earlier mapping wins, and the merge is shallow: a nested mapping is replaced, not combined. JSON has none of this, so a converter writes every alias out in full.

By the Velkaro team · Published

Anchors and aliases on lists

An alias stands for the whole node it points to; in JSON it becomes a full copy. This is GitLab’s own example of reusing a list of script lines. Converted to JSON, it shows what the YAML data really is: a list inside a list.

.default_scripts: &default_scripts
  - ./default-script1.sh
  - ./default-script2.sh
job1:
  script:
    - *default_scripts
    - ./job-script.sh
{
  ".default_scripts": [
    "./default-script1.sh",
    "./default-script2.sh"
  ],
  "job1": {
    "script": [
      [
        "./default-script1.sh",
        "./default-script2.sh"
      ],
      "./job-script.sh"
    ]
  }
}

GitLab accepts that nested list in script. A program that expects a flat list of strings will not, so check what the consumer of your file expects before reusing lists this way.

Which key wins

The merge key type inserts each merged key “unless the key already exists” in the mapping, and for a list of mappings it says: “Keys in mapping nodes earlier in the sequence override keys specified in later mapping nodes.” In practice:

base: &base {a: 1, b: 2}
before:
  b: local
  <<: *base
after:
  <<: *base
  b: local
{
  "base": {
    "a": 1,
    "b": 2
  },
  "before": {
    "b": "local",
    "a": 1
  },
  "after": {
    "a": 1,
    "b": "local"
  }
}

b: local wins whether it is written before or after <<. Only the key order in the JSON differs. With a list, the first mapping wins:

one: &one {color: red, size: S}
two: &two {color: blue, weight: 2}
merged:
  <<: [*one, *two]
{
  "one": {
    "color": "red",
    "size": "S"
  },
  "two": {
    "color": "blue",
    "weight": 2
  },
  "merged": {
    "color": "red",
    "size": "S",
    "weight": 2
  }
}

An anchor name can also be reused. The alias refers to the most recent anchor with that name above it, so the same *v gives two different values here:

first: &v 1
a: *v
second: &v 2
b: *v
{
  "first": 1,
  "a": 1,
  "second": 2,
  "b": 2
}

The merge is shallow

This is the easiest mistake to miss, because the file loads without an error. A job template sets two variables; a job merges the template and overrides one of them:

.job_template: &job
  image: ruby:3.3
  variables:
    RAILS_ENV: test
    DB: postgres
  script:
    - bundle exec rspec

test-mysql:
  <<: *job
  variables:
    DB: mysql
The converter output for test-mysql: variables contains only DB: mysql; RAILS_ENV from the template is gone.
In the JSON, test-mysql.variables is {"DB":"mysql"}. RAILS_ENV is gone, because the job’s own variables replaced the template’s whole mapping.

<< copies top-level keys only, so the job’s variables key exists already and the template’s is not inserted at all. To combine the two, anchor the inner mapping and merge it there too:

.job_template: &job
  image: ruby:3.3
  variables: &vars
    RAILS_ENV: test
    DB: postgres
  script:
    - bundle exec rspec

test-mysql:
  <<: *job
  variables:
    <<: *vars
    DB: mysql
{
  "test-mysql": {
    "image": "ruby:3.3",
    "variables": {
      "RAILS_ENV": "test",
      "DB": "mysql"
    },
    "script": [
      "bundle exec rspec"
    ]
  }
}

Lists are never merged. Docker’s Compose reference makes the same point: YAML merge only applies to mappings, not sequences. A job that sets its own script replaces the template’s list entirely.

The errors you will see

FileConverter message
deploy:
  <<: *defaults
  stage: deploy
defaults: &defaults
  image: alpine:3.20
Alias *defaults has no anchor before it. Define &defaults higher up in the same document: an alias cannot point forward, or across --- into another document.
defaults: &defaults
  image: alpine:3.20
---
job:
  <<: *defaults
Alias *defaults has no anchor before it. Define &defaults higher up in the same document: an alias cannot point forward, or across --- into another document.
tags: &tags [docker, linux]
job:
  <<: *tags
A << merge key takes a mapping or a list of mappings, not a list of values. To reuse a list, put its alias where the list goes.

The first two are the same rule: an anchor has to appear above its aliases in the same document. The specification says it is an error for an alias to use an anchor that “does not previously occur in the document”, and GitLab adds that anchors do not work across files brought in with include.

The converter showing an error: Alias *defaults has no anchor before it.
The forward reference in the converter. Moving the defaults block to the top of the file fixes it.

Merge keys are YAML 1.1, not 1.2

<< was defined as a separate YAML 1.1 type and is not part of the YAML 1.2 specification. The documentation of the yaml library says so directly, and turns merge keys off by default when it parses YAML 1.2. A strict 1.2 parser keeps << as an ordinary key:

base: &base {a: 1}
t:
  <<: *base
  b: 2
{
  "base": {
    "a": 1
  },
  "t": {
    "<<": {
      "a": 1
    },
    "b": 2
  }
}

Our converter applies merge keys in both its YAML 1.2 and YAML 1.1 modes, because the files that use them (Compose, GitLab CI) expect them to be merged. If your own program parses YAML 1.2 with default settings, test a file with << before relying on it.

Why converters cap alias expansion

Because every alias is copied in full, aliases of aliases grow fast. This 4-line file has 27 aliases and becomes 5,141 characters of minified JSON:

a: &a [1, 2]
b: &b [*a, *a, *a, *a, *a, *a, *a, *a, *a]
c: &c [*b, *b, *b, *b, *b, *b, *b, *b, *b]
d: &d [*c, *c, *c, *c, *c, *c, *c, *c, *c]

The classic “billion laughs” file uses ten levels of ten, which would expand to 109 copies. The yaml library that this converter uses has a limit for this, maxAliasCount, which defaults to 100; the converter raises it to 10,000 and stops with a “Too many aliases” message beyond that, so a hostile file cannot freeze the tab.

And back to YAML?

JSON to YAML does not invent anchors. Two identical objects are written out twice:

staging:
  image: alpine:3.20
  retries: 2
production:
  image: alpine:3.20
  retries: 2

If you want the anchors back, add them by hand after converting. Then run the file through YAML to JSON again and compare both JSON outputs in the JSON diff to confirm nothing changed.

Expand your own anchors

Sources

All pages were read on 10 November 2026. Converter outputs on this page are generated by the same code that runs in the converter.