YAML error
“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.
By yamltojsonfree · Published · Updated
What the parser is telling you
A colon followed by a space is how YAML separates a key from its value. When the scanner meets that pair while it is still reading a plain, unquoted value, it cannot start a second key inside a value, so it stops with this message. The column it reports is the offending colon.
The trap is that the colon it complains about is often not the mistake. A plain scalar can continue onto the next line, so if the line above is missing a colon, or the current line is indented deeper than it should be, the parser is still reading the previous value when it reaches your perfectly good key: value and reports it here.
Who prints this message
libyaml prints this, so it appears in everything built on it: kubectl and Helm through go-yaml, Docker Compose, Ruby and the GitLab CI linter through Psych, and Ansible or PyYAML when the C loader is installed. The pure-Python PyYAML loader prints the shorter “mapping values are not allowed here”; it is the same error.
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 colon inside an unquoted value
Any : in a value ends the value early. Text such as Deploy: production, Error: something or Note: see below all contain one.
Broken
title: Deploy: production
owner: platform teamFixed
title: "Deploy: production"
owner: platform teamFix: Quote the whole value. Colons not followed by a space are fine: http://host, 12:30 and C:\path parse without quotes.
A key indented deeper than the value above it
The line ports: 80 sits one space deeper than host, so the parser reads it as a continuation of localhost and then trips over its colon. libyaml reports the error on line 3 even though the mistake is the extra space.
Broken
server:
host: localhost
ports: 80Fixed
server:
host: localhost
ports: 80Fix: Sibling keys must share the same indentation. The same thing happens under a list item when a key is indented deeper than the one after the dash.
A missing colon on the first line
Without the colon, name api is a plain value that continues onto the next line, so the next line’s colon is a colon inside a value. When the missing colon is on a later line the message changes to “could not find expected ':'”.
Broken
name api
image: nginxFixed
name: api
image: nginxFix: The same applies to name:api with no space after the colon: YAML treats name:api as one word.
Find it fast
- 1.Go to the reported line and column. If the value there contains
:, quote the value. - 2.If the line looks correct, look at the line above it: a missing colon, no space after the colon, or one more space of indentation than its siblings.
- 3.Paste the document into the YAML validator on this site. It reports this error as “A value contains a colon, so YAML read it as a second key” with the line marked in the editor.
The same error in other parsers
| Parser | Message |
|---|---|
| libyaml, go-yaml (kubectl, Helm, Compose), Ruby Psych | mapping values are not allowed in this context |
| PyYAML, pure-Python loader | mapping values are not allowed here |
| js-yaml (Node, most browser tools) | bad indentation of a mapping entry |
| yaml (npm) | Nested mappings are not allowed in compact mappings |
| SnakeYAML (Java) | mapping values are not allowed here |
| This site’s validator | A value contains a colon, so YAML read it as a second key. |
Frequently asked questions
Why does the error point at a line that looks correct?
Because the parser was still reading the value from the line above. A plain, unquoted YAML value can continue onto following lines, so a missing colon or an extra space of indentation on line 2 is only noticed when the colon on line 3 turns up inside that value. Check the line above the one reported.
Do I have to quote every value that contains a colon?
Only when the colon is followed by a space or ends the value. URLs such as http://example.com, times such as 12:30 and Windows paths such as C:\Users parse without quotes. Quoting them anyway is harmless and protects you from YAML 1.1 reading 12:30 as the number 750.
Is “mapping values are not allowed here” the same error?
Yes. It is the wording of the pure-Python PyYAML scanner; libyaml and its ports, including go-yaml behind kubectl and Helm, say “in this context”. The cause and the fix are identical.
Related errors
“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.
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“bad indentation of a mapping entry”
js-yaml reports several unrelated mistakes under this one message: an unquoted value containing a colon, a sibling key indented differently, a key nested under a key that already has a value, or a value starting with @ or a backtick. Check the reported line for each, in that order.
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