Code Beautifier
duplicated mapping key

Fix YAML "duplicated mapping key"

YAML forbids the same key twice in one mapping. Why the parser rejects it instead of keeping the last value, how duplicates creep in, and how to fix them.

Input that triggers it

database:
  host: db.internal
  port: 5432
  host: db.backup
Open YAML Validator on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

yaml
json

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=1 and env: B=2 on separate lines, when the intent was a list of two environment entries.
  • Case or spacing that hides the duplicate. Host and host are different keys; host twice is not. The validator reports only the true duplicates.

How to fix it

  1. Paste the document into the validator above. The error card gives the line of the second occurrence.
  2. Decide which value is correct and delete the other, or rename one key if both values are genuinely needed for different purposes.
  3. If the values belong together, restructure them as a list:
database:
  hosts:
    - db.internal
    - db.backup
  port: 5432
  1. 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.labels or env blocks 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