error converting YAML to JSON: yaml: line N: did not find expected key (Kubernetes) — how to fix
error: error parsing deployment.yaml: error converting YAML to JSON: yaml: line 18: did not find expected key
Quick fix: Open the file at the reported line and the lines above it. Align sibling keys at exactly the same indentation using spaces only, keep - list items consistent, and close any open ", ', [ or {. Validate with kubectl apply --dry-run=client -f file.yaml or a YAML linter.
What it means
kubectl (and Helm, and anything using the Go yaml library) parses YAML before converting it to JSON. While inside a block mapping, the parser expects the next line at the same indentation to start with key:. When it finds something else (a line indented differently, a stray -, or text still inside an unclosed quote), it stops with "did not find expected key".
The line number is where the parser gave up. The actual mistake is often on that line or a few lines above it. You can paste the file into the YAML validator to see the error position.
Cause 1: inconsistent indentation
spec:
containers:
- name: web
image: nginx
ports: # 5 spaces instead of 6
- containerPort: 80
Sibling keys must start in the same column:
spec:
containers:
- name: web
image: nginx
ports:
- containerPort: 80
Turn on "render whitespace" in your editor. YAML forbids tab characters for indentation, so replace tabs with spaces (grep -nP '\t' file.yaml finds them).
Cause 2: list item mixed into a mapping
env:
- name: MODE
value: prod
name: DEBUG # missing "- "
value: "false"
Every item in the list needs its own dash at the same column:
env:
- name: MODE
value: prod
- name: DEBUG
value: "false"
Cause 3: unclosed quote or bracket
args: ["--port", "8080]
command: "sh -c 'echo hi"
An unclosed quote or bracket swallows the following lines, so the error is reported far below the real problem. Close them, or switch to block style for long strings:
command:
- sh
- -c
- |
echo "starting"
exec nginx -g 'daemon off;'
Cause 4: values starting with special characters
A value that starts with *, &, !, %, @, `, { or [ has special meaning in YAML. Quote such values:
schedule: "*/5 * * * *"
password: "@dm1n"
Cause 5: Helm templates rendering bad indentation
With Helm, the YAML is generated, so the line number refers to the rendered output. Render it and look there:
helm template myrelease ./chart > rendered.yaml
helm template myrelease ./chart --debug
The usual culprit is toYaml without the right indent:
resources:
{{ toYaml .Values.resources | indent 2 }} # fragile
# better:
resources:
{{- toYaml .Values.resources | nindent 2 }}
nindent starts a new line before indenting, so the first line lines up as well.
Validate before applying
kubectl apply --dry-run=client -f deployment.yaml
yamllint deployment.yaml # pip install yamllint
yamllint reports indentation and syntax problems with exact positions, which is quicker than guessing from the kubectl message.
Last updated 2026-12-03.