kubectl "error parsing ...: error converting YAML to JSON" in CI - Fix it
kubectl could not parse the file as YAML before it ever reached the cluster. A tab character, misaligned indentation, an unquoted special value, or leftover template markup makes the YAML-to-JSON conversion fail at a specific line.
What this error means
A kubectl apply -f fails with error: error parsing deploy.yaml: error converting YAML to JSON: yaml: line 14: did not find expected key.
kubectl
error: error parsing deploy.yaml: error converting YAML to JSON: yaml: line 14:
did not find expected keyCommon causes
Tabs or inconsistent indentation
YAML forbids tabs for indentation; a stray tab or a mix of indent widths breaks parsing at that line.
Unrendered template markup or unquoted values
Leftover {{ ... }} from a templating step, or an unquoted value containing : or @, produces invalid YAML.
How to fix it
Validate the YAML at the reported line
- Open the file at the
line Nfrom the message and fix the indentation or stray tab. - Quote values that contain special characters.
- Confirm any templating step fully rendered before kubectl ran.
Terminal
# show non-printing characters to spot tabs
kubectl apply -f deploy.yaml --dry-run=clientLint manifests in CI before applying
A YAML lint or client-side dry run catches parse errors as a fast gate.
Terminal
kubectl apply -f k8s/ --dry-run=client -o yaml > /dev/nullHow to prevent it
- Use spaces, never tabs, for YAML indentation.
- Render templates fully before handing manifests to kubectl.
- Add a YAML lint or
--dry-run=clientgate in CI.
Related guides
kubectl "error validating data: ValidationError ... unknown field" in CI - Fix itFix kubectl "error validating data: ValidationError(...): unknown field" in CI - the manifest has a field the…
kubectl kustomize "no matches for Id" build error in CI - Fix itFix kubectl/kustomize "no matches for Id ...; failed to find unique target for patch" in CI - a patch or name…
kubectl apply "field is immutable" on an existing resource in CI - Fix itFix kubectl apply "The Deployment ... is invalid: spec.selector: Invalid value: ... field is immutable" in CI…