What the error means
A YAML mapping is a set of unique keys. When the same key appears twice at the same level, the document is ambiguous: should the second value replace the first, or is it a mistake? The YAML specification says the keys must be unique, so a strict parser refuses to guess and reports duplicated mapping key with the line of the second occurrence.
In the example, host appears twice under database. A lenient parser would silently keep db.backup and drop db.internal; a strict one stops here. The strict behaviour is the useful one, because a silently dropped value is exactly how a service ends up pointed at the wrong database.
Why it happens
- A merge that kept both sides. Resolving a conflict in a config file by accepting both changes produces two copies of the same key one after another.
- Copy-and-paste extension. A block is duplicated to add a second entry, but the key that should have changed did not.
- A key that is meant to be a list. Writing
env: A=1andenv: B=2on separate lines, when the intent was a list of two environment entries. - Case or spacing that hides the duplicate.
Hostandhostare different keys;hosttwice is not. The validator reports only the true duplicates.
How to fix it
- Paste the document into the validator above. The error card gives the line of the second occurrence.
- Decide which value is correct and delete the other, or rename one key if both values are genuinely needed for different purposes.
- If the values belong together, restructure them as a list:
database:
hosts:
- db.internal
- db.backup
port: 5432
- Validate again. A clean pass reports the document count and the top-level keys.
Converting the fixed document with YAML to JSON is a good final check: JSON objects cannot hold duplicate keys either, so if the conversion looks right, the structure is unambiguous.
If it still fails
- Kubernetes manifests frequently trip this in
metadata.labelsorenvblocks after a templating step. Validate the rendered output, not the template. - If the duplicate is inside a document separated by
---, remember that each document is validated on its own; the line number counts from the start of the file, not the document. - Some tools rely on last-key-wins behaviour deliberately. If yours does, this validator will still flag it, because the file is not portable to a strict parser — and that is worth knowing before it moves between systems.
Related errors
bad indentation of a mapping entry
Every key in the same mapping must start in the same column, indented with spaces only. Align the offending line with its siblings and replace any tab with spaces.
end of the stream or a document separator is expected
Separate multiple documents with a --- line, or nest the extra content under a key. Each YAML document may hold only one top-level value.