yaml did not find expected key: Fix error converting YAML to JSON (2026)

Published March 1, 2026 · 7 min read

Intent match: this page is for parser failures like yaml did not find expected key and error converting YAML to JSON in Docker Compose, Kubernetes manifests, and CI YAML files.

Table of contents

  1. Single decision path for expected-key failures
  2. Runtime verification checks (Compose, Kubernetes, CI)
  3. FAQ

1. Single decision path for did not find expected key

Start at Branch A and stop when the parser succeeds. This avoids random edits and pinpoints root cause quickly.

Branch A: Indentation errors

Signals: line numbers shift between runs, block lists break, or tabs appear in editor whitespace view.

  • Compose: misaligned service keys under services: or environment:.
  • Kubernetes: list items under containers:, env:, or ports: not aligned.
  • CI: job/step blocks under jobs: or steps: drift by one space.
rg -n "\t" docker-compose*.yml k8s/*.yaml .github/workflows/*.yml
# replace tabs with spaces and realign sibling keys

Branch B: Missing colon or key mapping errors

Signals: parser points near a key name, or the previous line has text that looks like a key but lacks :.

  • Compose: image nginx:1.27 instead of image: nginx:1.27.
  • Kubernetes: name app instead of name: app in metadata or env entries.
  • CI: runs-on ubuntu-latest instead of runs-on: ubuntu-latest.
# quick parse checks after fixing missing colons
yamllint docker-compose.yml manifest.yaml .github/workflows/ci.yml

Branch C: Quoted-value or type mistakes

Signals: values include :, #, booleans, or numeric-like strings that parse unexpectedly.

  • Compose: unquoted URL or host:port values in environment mappings.
  • Kubernetes: labels/annotations with special characters not wrapped in quotes.
  • CI: expression-like strings or colon-separated values parsed as mappings.
# bad
API_URL: https://api.example.com:v1
# good
API_URL: "https://api.example.com:v1"

After quoting or type correction, rerun the runtime parser check below before merge/deploy.

2. Runtime verification checks (Compose, Kubernetes, CI)

Run at least the check that matches your runtime, and keep the output in CI logs.

# Docker Compose
docker compose config -q

# Kubernetes manifests
kubectl apply --dry-run=client -f manifest.yaml

# Generic CI gate
yamllint .

If any command still fails with error converting YAML to JSON, return to Branch A and re-check indentation first.

3. FAQ

Why does this error appear even when line numbers look correct?

YAML parser line reporting often points near the failure, not always the exact root line. Check a few lines above first.

Should I fix by converting YAML to JSON first?

No. Conversion fails for the same syntax reasons. Fix YAML structure first, then convert if needed.

Is this usually a runtime problem or syntax problem?

It is almost always syntax/structure. Runtime validation only happens after the parser succeeds.