YAML
JSON
Kubernetes
error handling
data conversion

Fix "error converting YAML to JSON: did not find expected key" in Kubernetes

Codemia Knowledge Hub ·

"did not find expected key" means the YAML parser hit a token it did not expect because the document's structure is broken, usually from improper indentation, a missing colon, or a wrong key-value relationship. Kubernetes (and tools such as Helm) convert your YAML manifest to JSON before sending it to the API, so the error appears at that conversion step. Check the line number in the message and the lines just above it.

How a manifest maps from YAML to JSON

YAML expresses hierarchy through indentation; JSON uses braces and brackets. This Pod manifest:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: my-pod
5spec:
6  containers:
7    - name: my-container
8      image: my-image

becomes this JSON:

json
1{
2  "apiVersion": "v1",
3  "kind": "Pod",
4  "metadata": {
5    "name": "my-pod"
6  },
7  "spec": {
8    "containers": [
9      {
10        "name": "my-container",
11        "image": "my-image"
12      }
13    ]
14  }
15}

If the indentation is off, the parser cannot work out which key a line belongs to.

Common causes and fixes

Indentation errors

Incorrect indentation breaks the hierarchy:

yaml
1   spec:
2   containers:  # Incorrect indentation level
3     - name: my-container
4       image: my-image

Indent child keys consistently under their parent:

yaml
1   spec:
2     containers:
3       - name: my-container
4         image: my-image

Missing colons or stray punctuation

A key without a colon, or extra characters, is another frequent cause:

yaml
   metadata
     name: my-pod

Make sure every key is followed by a colon:

yaml
   metadata:
     name: my-pod

Inconsistent data types

Mixing types can also cause problems when converting YAML to JSON:

yaml
   ports:
     - port: "80"  # Quoting numbers suggests a string
     - port: 443

Keep the types consistent:

yaml
   ports:
     - port: 80
     - port: 443

Validating YAML before you apply it

  • YAML Lint validates YAML syntax and catches structural issues.
  • yq is a command-line processor that handles YAML and can convert it to JSON.
  • Online validators give real-time syntax checking.

You can also convert programmatically with Python's PyYAML, which reports a parse error instead of producing bad output:

python
1import yaml
2import json
3
4yaml_content = '''
5apiVersion: v1
6kind: Pod
7metadata:
8  name: my-pod
9'''
10
11try:
12    json_content = json.dumps(yaml.safe_load(yaml_content), indent=2)
13    print(json_content)
14except yaml.YAMLError as e:
15    print("Error loading YAML:", e)

To catch these errors before they reach a cluster, add YAML validation and conversion checks to your CI/CD pipeline, and test configuration files in a staging environment.

CauseDescriptionSolution
Indentation ErrorsMisaligned spaces or tabsEnsure consistent indentation
Missing ColonsAbsence of a colon after a keyInclude colons after each key
Inconsistent Data TypesMixing strings and numbers unintentionallyMaintain consistent data types
Manual Conversion ErrorsHuman error during manual conversionUse reliable automated tools

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design