Skip to content
Latchkey

kubectl "error parsing STDIN: error converting YAML to JSON" in CI

You piped a manifest into kubectl apply -f - and the bytes on STDIN are not valid YAML. The conversion to JSON fails before anything reaches the API server - usually a broken heredoc, bad templating output, or interpolated values that corrupt the indentation.

What this error means

cat ... | kubectl apply -f - (or a heredoc) fails with error: error parsing STDIN: error converting YAML to JSON: yaml: line N: .... The same template applied from a file would show the same line - the problem is the generated YAML, not the pipe.

kubectl output
error: error parsing STDIN: error converting YAML to JSON: yaml: line 8:
could not find expected ':'

Common causes

Broken heredoc or interpolation

An unquoted shell variable expands to a multi-line or special value that breaks indentation, or a heredoc terminator is mis-indented, producing invalid YAML on STDIN.

Templating emitted malformed YAML

envsubst/sed/Helm/kustomize output a value with a stray colon, tab, or unescaped character, so the piped document does not parse.

How to fix it

Inspect the generated YAML before piping it

Render to a file (or print it) so you can see line N exactly as kubectl receives it.

Terminal
envsubst < template.yaml | tee /tmp/rendered.yaml | sed -n '5,11p' | cat -A
kubectl apply -f /tmp/rendered.yaml

Quote interpolated values and fix the heredoc

  1. Quote shell-substituted values so colons, spaces, and newlines do not break YAML.
  2. Use a quoted heredoc (<<'EOF') when you do not want shell expansion inside it.
  3. Lint the rendered output (yamllint) instead of piping unchecked.

How to prevent it

  • Render templates to a file and lint before applying, rather than piping blind.
  • Quote interpolated values in manifests and use quoted heredocs where appropriate.
  • Validate with kubectl apply --dry-run=client -f - in CI.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →