kubectl apply rejects a manifest with cannot unmarshal into Go struct field
Your error
Service in version "v1" cannot be handled as a Service: json: cannot unmarshal object into Go struct field ServiceSpec.spec.ports of type []v1.ServicePort2 more lines it can showFix it fast
Most common cause, 1 of 3A list field was written as a map
Put a dash in front of each item so the field becomes a list of objects.
spec:
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080Short answer
The API server decoded your YAML and one field had the wrong shape. cannot unmarshal object into a []v1.ServicePort field means a list written without dashes; number or bool into type string means an unquoted value like 8080 or true; array into a map means dashes where a map belongs. Fix that field. Applies to Kubernetes 1.37.
Which cause do you have?
The message names three things: the JSON kind it found (object, array, number, bool), the field path, and the Go type it expected. Ask kubectl what that field should look like, using the path from the message.
kubectl explain service.spec.portsThen find the line you see in this list and open its fix:
- 1A list field was written as a map
The error says cannot unmarshal object and the expected type starts with [] - 2A string field got an unquoted number or boolean
The error says cannot unmarshal number or cannot unmarshal bool and ends with of type string - 3A map field was written as a list
The error says cannot unmarshal array and the expected type is map[string]string
What does the error look like?
Service in version "v1" cannot be handled as a Service: json: cannot unmarshal object into Go struct field ServiceSpec.spec.ports of type []v1.ServicePortjson: cannot unmarshal number into Go struct field EnvVar.spec.template.spec.containers.env.value of type stringjson: cannot unmarshal array into Go struct field ObjectMeta.metadata.labels of type map[string]string
Cause 1 of 3
A list field was written as a map
You seeThe error says cannot unmarshal object and the expected type starts with []
Check
When Kubernetes reports cannot unmarshal object into a type that begins with [], the field takes a list, but the YAML gave it key-value pairs. Service ports, container lists and env are all arrays in the API reference, so each entry needs a leading dash.
kubectl apply --dry-run=server -f service.yamlFix
Put a dash in front of each item so the field becomes a list of objects.
spec:
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080Verify
The server-side dry run reports the Service as created or configured, and no unmarshal error appears.
kubectl apply --dry-run=server -f service.yamlConfirmed by kubernetes.io
Cause 2 of 3
A string field got an unquoted number or boolean
You seeThe error says cannot unmarshal number or cannot unmarshal bool and ends with of type string
Check
YAML reads 8080, 1.0 and true as a number or boolean, not text. EnvVar value, label values and annotation values are strings in the API, so an unquoted number or flag there fails decoding with this message.
grep -nE "value: [0-9]|value: (true|false)" deployment.yamlFix
Wrap the value in quotes. Kubernetes guidance is to quote anything that could be read as a boolean, and the same rule keeps numbers as text.
env:
- name: PORT
value: "8080"
- name: FEATURE_FLAG
value: "true"Verify
The dry run passes and the running pod shows the variable as text.
kubectl exec deploy/web -- printenv PORTConfirmed by kubernetes.io
Cause 3 of 3
A map field was written as a list
You seeThe error says cannot unmarshal array and the expected type is map[string]string
Check
Labels, annotations, selectors and matchLabels are maps of string keys to string values. Writing them with dashes turns them into a list, which the decoder cannot place into a map.
kubectl explain deployment.spec.selector.matchLabelsFix
Remove the dashes so each label is a plain key: value line under the map.
metadata:
labels:
app: web
tier: frontendVerify
The object applies and the labels are visible on it.
kubectl get deploy web --show-labelsConfirmed by kubernetes.io
How do I stop it coming back?
- Run kubectl apply --dry-run=server on every changed manifest in CI before merging.
- Quote every env, label and annotation value in templates, even when it looks like a number.
- Check field shapes with kubectl explain instead of copying fragments from older manifests.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.