如何解析ruby中不规范的字符串JSON?

How to parse string that are not standardized JSON in ruby?

我正在使用 fluentd 来解析日志,它看起来像:

{ date="2017-04-01 10:22:18.306", message="This is a trace Message!" }
{ date="2017-04-01 10:22:18.306", message="This is a debug message" }

虽然标准化 JSON 版本应该是:

{ "date":"2017-04-01 10:22:18.306", "message":"This is a trace Message!" }

我试过了

str='{ date="2017-04-01 10:22:18.306", message="This is a trace Message!" }'
Yajl::Parser.parse(str)

它不起作用:

Yajl::ParseError: lexical error: invalid char in json text.
                                     { date="2017-04-01 10:22:18.306",
                     (right here) ------^

    from /var/lib/gems/2.3.0/gems/yajl-ruby-1.2.1/lib/yajl.rb:37:in `parse'
    from /var/lib/gems/2.3.0/gems/yajl-ruby-1.2.1/lib/yajl.rb:37:in `parse'
    from (irb):45
    from /usr/bin/irb:11:in `<main>'

您可以将 scan 与正则表达式一起使用:

data = %q(
{ date="2017-04-01 10:22:18.306", message="This is a trace Message!" }
{ date="2017-04-01 10:22:18.306", message="This is a debug message" }
)

pattern = /date="([^"]+)", message="([^"]+)"/

messages = data.scan(pattern).map{ |date, message|
  {date: date, message: message}
} 

p messages
# [{:date=>"2017-04-01 10:22:18.306", :message=>"This is a trace Message!"}, {:date=>"2017-04-01 10:22:18.306", :message=>"This is a debug message"}]