The error message Unable to parse YAML: mapping values are not allowed here
usually occurs when there is a syntax error in the YAML file.
To resolve this issue, we can consider following the steps below:
YAML relies on proper indentation and syntax. We need to ensure that our YAML file follows the correct syntax rules. Let's see some of the rules that are necessary to incorporate in our YAML file:
We need to use spaces for indentation instead of tabs. Giving an indentation of two or four spaces is preferable, but we must ensure consistent indentation levels. Below, we can see that the YAML file is incorrect at line 3 due to the wrong indentation of subkey2
:
key1:subkey1: value1subkey2: value2
We resolve the error by correctly indenting subkey2
with two spaces:
key1:subkey1: value1subkey2: value2
We must ensure that key-value pairs are properly formatted with a colon (:) separating the key and value. We can see in the code below that the colon (:) is missing after key2
, which will cause an error:
key1: value1key2subkey1: value2
We correct the error by adding the colon (:) as shown below:
key1: value1key2:subkey1: value2
We must ensure that we do not add extra colons, commas, or punctuation marks to the file. Below we see a YAML file that causes errors at:
Line 1: Due to an unnecessary colon after value1
.
Line 4: Due to an unnecessary comma after subkey2
instead of a colon.
Line 8: Due to an unnecessary comma after item3
instead of a colon.
key1: value1:key2:subkey1: subvalue1subkey2, subvalue2key3:- item1- item2- item3,
We can see below that the file is corrected after resolving the errors mentioned above:
key1: value1key2:subkey1: subvalue1subkey2: subvalue2key3:- item1- item2- item3
We need to properly format each list item in a sequence (list) with a dash (-
) followed by a space. We can see below that in line 6, the list item item4
is not correctly formatted:
key1:- item1- item2key2:- item3item4
We correct the error by adding a dash (-
) and a space before item
, as shown below:
key1:- item1- item2key2:- item3- item4
We must remove strange Unicode characters in our YAML file, which may cause the mapping values are not allowed here
error. Most of the strange Unicode characters can be removed by an eyeball, but there are spaces other than the regular space that may cause an error. There are multiple spaces like U+2009(thin space), U+200A (hair space), and U+200B (zero width space). To remove these spaces, we can download the Gremlins extension in VS Code that reveals the invisible whitespaces and other Unicode characters.
Once we ensure that our YAML file is correctly structured according to the rules mentioned above, our error message Unable to parse YAML: mapping values are not allowed here
will be resolved. If we cannot find any syntax issues, use an online YAML validator and correct the problems it identifies.
Free Resources