/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:
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
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
labels: {}Quote values that contain ": "
# wrong: the parser sees a second mapping inside the value
command: ["sh", "-c", echo ready: yes]
# right
command: ["sh", "-c", "echo ready: yes"]Sources
Related pages
errors · helmHelm: nil pointer evaluating interface {}.valuehelm template and helm install fail with nil pointer evaluating interface {} when a template reads a nested value whose parent key is missing from values.yaml.commands · kubernetesGenerate Kubernetes YAML with kubectl create --dry-run=client -o yamlkubectl create deployment, service, configmap or secret with --dry-run=client -o yaml prints a ready manifest without a cluster. Save it with > file.yaml.cheat sheets · yamlYAML cheat sheet: syntax, multiline strings and gotchasYAML cheat sheet: maps, lists, scalars, block strings with | and >, anchors and aliases, and the YAML 1.1 traps where no becomes false and 012 is octal.