-2

From zabbix-api login I have this output:

{"jsonrpc":"2.0","result":"4b79a399f043fa44c5653bee3ecb346d","id":0} 

I'm trying to parse using the code above in ruby:

command_out = shell_out(command).stdout.to_s
node.default['zabbix_server']['zabbix_auth'] = command_out.lines.grep(/"(result)":"((\\\"|[^"])*)"/)

how can I grab only the "4b79a399f043fa44c5653bee3ecb346d" ?

Gsk
  • 2,929
  • 5
  • 22
  • 29
Daniel
  • 95
  • 4
  • 12

1 Answers1

0

You are actually handling a JSON, which is a data format like XML or YAML.
Luckily enough, you can rely on the JSON gem, developed to parse a standard JSON.

Here is how you can use it:

require 'JSON'

my_json = '{"jsonrpc":"2.0","result":"4b79a399f043fa44c5653bee3ecb346d","id":0}'

a = JSON.parse(my_json)
p a['result']

and the output is:

"4b79a399f043fa44c5653bee3ecb346d"

Gsk
  • 2,929
  • 5
  • 22
  • 29