Search code examples
rubyyamlpsychparser

YAML: error parsing a string containing a square bracket as its first character


I'm parsing a YAML file in Ruby and some of the input is causing a Psych syntax error:

require 'yaml'

example = "my_key: [string] string"
YAML.load(example)

Resulting in:

Psych::SyntaxError: (<unknown>): did not find expected key
          while parsing a block mapping at line 1 column 1
from [...]/psych.rb:456:in `parse'

I received this YAML from an external API that I do not have control over. I can see that editing the input to force parsing as a string, using my_key: '[string] string', as noted in "Do I need quotes for strings in YAML?", fixes the issue however I don't control how the input is received.

Is there a way to force the input to be parsed as a string for some keys such as my_key? Is there a workaround to successfully parse this YAML?


Solution

  • One approach would be to process the response before reading it as YAML. Assuming it's a string, you could use a regex to replace the problematic pattern with something valid. I.e.

    resp_str = "---\nmy_key: [string] string\n"
    re = /(\: )(\[[a-z]*?\] [a-z]*?)(\n)/
    resp_str.gsub!(re, "#{$1}'#{$2}'#{$3}")
    #=> "---\n" + "my_key: '[string] string'\n"
    

    Then you can do

    YAML.load(resp_str)
    #=> {"my_key"=>"[string] string"}