psych: wrong line reported in 'did not find expected key while parsing a block mapping'

When parsing this yaml

key_one:
  inner:
  - value1
  - value2

key_two:
    inner:
  - value1
  - value2

the error message

Psych::SyntaxError: (/tmp/oops.yaml): did not find expected key while parsing a block mapping at line 1 column 1

contains the wrong line number. It should be reporting line 7 column 5.

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Reactions: 102
  • Comments: 64

Commits related to this issue

Most upvoted comments

To anyone stumbling through the internet that finds this issue for related reasons: Somewhere in your YAML you have extra spaces (or not enough spaces) before a key value. Thank you all for helping me solve this on a completely different project!

Edit on December 10, 2019: A great YAML linter may help here: https://jsonformatter.org/yaml-formatter.

+1, got bitten by this issue today. Very annoying when the syntax error occured nearly 1500 lines into the file!

For anyone who is looking for this stupid error, I fix error by removing " ’ " character.

(WHITESPACE AS SYNTAX IS THE DUMBEST THING EVER), thank you

+1 on the empty space in the yml file. Copy/paste error.

so it was a bad copy for me.

In vim I did a search of ^\(\s\s\)\+ to find all instances of good spacing and was able to find my bug

Save me a lot of time. And here is a yamllint tool. http://www.yamllint.com/

Here’s command-line yamllint. It gives the correct line number.

https://github.com/adrienverge/yamllint

When this happens to me, I use this tool it tells you the line that is wrong:

https://yamlchecker.com/

@ORESoftware that’s the whole point of using YAML. Use JSON instead.

@pooja-mane Can you try something:

  • delete the lines where the problem seems to happens
  • check that you can launch your server after deleting (if so, you have spotted where are the ‘wrong’ lines)
  • rewrite them by hand
  • be careful with “'” and ‘"’ : use " mostly if possible
  • I had this issue after bulk changing all my " to '; and then there was a “If you didn"t request” so you see my mistake was very small indeed ^^

Had the same issue on Windows 10 today. I used https://yamlchecker.com/ as recommended by @WaKeMaTTa

Resolution: No double quotes needed for Windows directory.

For me the line number was incorrect when I had unescaped double quotes inside a value…

@elcortez thank you. I got my mistake in code. create_list: “Create a List” message: “Save Profiles” as I was using massage key as create_list.message which is wrong. as when it searches for for create_list it got a label “Create a List” it wont go to search for message key. I changed my code as create_list: message: “Save Profiles”

Here’s command-line yamllint. It gives the correct line number. https://github.com/adrienverge/yamllint

Thank you @mahemoff, you just saved me a world of pain!

@illepic Thank you so much! I was missing an extra space.

I guess it’s winding back to the start of the structure it’s trying to parse, rather than flagging the error then and there…

Wow. Error was fixed when I trimmed the lines and got rid of spaces, wtf

Thanks @illepic!

I ran into the same problem and decided to open an issue https://github.com/travis-ci/travis-yaml/issues/128

Had the same issue, but instead of wrong whitespaces I got comments inside a single command like the following:

  script:
    - if true; then
         # comment
         echo hello;
      fi

The travis lint reported error on line 2, column 1 or on line 1, column 1 during my experiments. While error was on some 23 line 😕 I’m glad I solved this puzzle 😃

I saw this issue (“line 1, column 1”) do to a comma at the end of the line after converting json by hand.

E.g.

foo:
- "bar bah",
- "asdf asdf"

Super glad I found this issue. Probably saved my an hour of hair pulling. In my case, it seems to be reporting the correct column number, but still reports line 1 incorrectly.