YAML error
“could not find expected ':'”
A line that sits where a key belongs has no colon on it. Usually the colon is missing or has no space after it, or a long value wrapped onto a new line without quotes. Add the colon, or quote or block-format the value.
By yamltojsonfree · Published · Updated
What the parser is telling you
When a line starts at the indentation of a mapping, the scanner assumes it is reading a key and looks for : on the same line, because a key must fit on one line. If the line ends without one, the scanner waits, and when the next line starts it reports that the key it began scanning never got its colon. The “while scanning a simple key” location is the line that is wrong; the second location is where the parser noticed.
The same mistake on the very first line of a file produces a different message, “mapping values are not allowed in this context”, because no mapping has been opened yet. Deeper in the file it produces this one.
Who prints this message
PyYAML, libyaml and go-yaml all use this wording, so it appears from Python, Ruby, kubectl, Helm and Docker Compose. The full message starts with “while scanning a simple key”.
The causes, with fixes
Broken YAML on the left, the corrected form on the right. The fix under each pair is the one that applies to that cause.
A missing colon
The line image nginx has no colon, so it cannot be a key. The parser reports the following line, replicas: 3, as the place it gave up.
Broken
name: api
image nginx
replicas: 3Fixed
name: api
image: nginx
replicas: 3Fix: Also check for a colon with no space after it: image:nginx is one plain word to YAML, not a key and a value.
A long value wrapped onto the next line
The second line starts at the key column, so YAML reads it as a new key rather than a continuation, and it has no colon.
Broken
description: Restarts the worker when the
queue backs up
owner: platformFixed
description: >
Restarts the worker when the
queue backs up
owner: platformFix: Use > for folded text or | to keep line breaks, and indent the continuation lines. Quoting the whole value also works when the continuation is indented.
A missing colon inside a list item
The same rule applies inside a sequence entry: every line at the item’s indentation is a key and needs its colon.
Broken
containers:
- name: api
image nginx
ports: [80]Fixed
containers:
- name: api
image: nginx
ports: [80]Fix: The reported line is ports, one below the mistake.
Find it fast
- 1.Read the first location in the full message, after “while scanning a simple key”. That is the line without a colon.
- 2.If the line is meant to be a value rather than a key, indent it under its key or switch the value to
>or|block style. - 3.Paste the document into the YAML validator on this site; it reports “A key spans more than one line” with the line marked.
The same error in other parsers
| Parser | Message |
|---|---|
| PyYAML, libyaml, go-yaml (kubectl, Helm, Compose), Ruby Psych | could not find expected ':' |
| js-yaml (Node, most browser tools) | end of the stream or a document separator is expected |
| yaml (npm) | Implicit keys need to be on a single line |
| This site’s validator | A key spans more than one line. |
Frequently asked questions
The line it reports has a colon. What is wrong?
The reported line is where the parser noticed, one below the real problem. The line above it is the one without a colon, or with a colon that is not followed by a space. The full PyYAML and libyaml message prints both locations; the first one, after “while scanning a simple key”, is the mistake.
Can a value span several lines without quotes?
Yes, if the continuation lines are indented further than the key. A plain value that continues at the key’s own column is read as a new key. For anything longer than a line, use a block scalar: > folds the lines into one paragraph, | keeps the line breaks.
Why does Helm report this on a line that does not exist in my template?
Helm reports lines in the rendered output, not the template. A template expression that renders to an empty string, or an include without nindent, can leave a line without a colon in the output. Run helm template with --debug to see the rendered YAML and count lines there.
Related errors
“mapping values are not allowed in this context”
The parser hit a colon followed by a space in the middle of a value, where YAML only allows a colon after a key. Quote the value if the colon is part of it, or fix the line above if the parser was still reading it.
Read the fix“did not find expected key”
The parser was inside a mapping and expected either another key at the same indentation or the end of the block, but found something that cannot be a key: a line dedented by the wrong amount, a list dash, or an invisible character. Line up the indentation of the line it reports with its siblings.
Read the fix“error converting YAML to JSON”
kubectl does not parse YAML directly. It converts each document to JSON first, and this wrapper means that conversion failed because the YAML is malformed. The real error is the part after “yaml: line N:”, and N counts from the start of that document, not from the top of the file.
Read the fixAll six underlying mistakes, ordered by cause rather than by message, are in the guide Common YAML errors and how to fix them. Every one of them is caught with the exact line and a suggested fix by the free tools below; nothing is uploaded.
YAML to JSON
Convert YAML into formatted or minified JSON, with errors pinned to the exact line.
OpenJSON to YAML
Turn JSON back into readable YAML, with control over indentation and key order.
OpenYAML Validator
Check YAML for syntax errors and get a plain-English explanation of what went wrong.
OpenYAML Formatter
Reformat messy YAML with consistent indentation and keep your comments intact.
Open