如何解析ruby中非标准化JSON的字符串?

时间:2017-04-12 09:22:06

标签: json 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>'

1 个答案:

答案 0 :(得分:1)

您可以将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"}]