Skip to content

/errors/kubernetes/error-converting-yaml-to-json

kubectl: error converting YAML to JSON: mapping values are not allowed in this context

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Client Version: v1.37.1updated 2026-10-20
tool
Kubernetes
exit code
1
versions
kubectl 1.x
os
any

kubectl parses manifests as YAML and converts them to JSON before sending them to the API server. When the YAML is malformed, it stops with error converting YAML to JSON: yaml: line N: mapping values are not allowed in this context. Line N is where the parser noticed the problem; the mistake is usually on that line or the one just before it, and is almost always indentation.

Reproduce it

labels: is indented three spaces instead of two, so it looks like a continuation of name: app-config:

bash
cat > bad.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
   labels: {}
EOF
kubectl apply --dry-run=client -f bad.yaml
echo "exit status: $?"

Output

error: error parsing bad.yaml: error converting YAML to JSON: yaml: line 5: mapping values are not allowed in this context
exit status: 1

--dry-run=client parses the file locally, so this check works without a cluster.

Fix the indentation

yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  labels: {}

Quote values that contain ": "

yaml
# wrong: the parser sees a second mapping inside the value
command: ["sh", "-c", echo ready: yes]

# right
command: ["sh", "-c", "echo ready: yes"]

Sources