How to parse a json output

Hi
I am trying to read about json output , buts its getting little bit of
hard

url = “http://xxxxxxx:111/v1=rebuild
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
puts data =:::::::::::
{“BD51-5”:276611,“TOTAL”:1459071,“BD51-10”:1182460}
result = JSON.parse(data)
puts result =:::::::::::::: BD51-101182460TOTAL1459071BD51-5276611

Basically,

I want to see, if the output of “result” or “data” has BD51-5 or not
. ( or the search could be, if BD51-11 is there or not )

i know that JSON.parse puts everything as key->value … but problem is,
I dont know the key of hash…

how can i get what i want to ?
Thanks for your help

{“BD51-5”:276611,“TOTAL”:1459071,“BD51-10”:1182460}
I want to see, if the output of “result” or “data” has BD51-5
or not
but problem is, I dont know the key of hash…

BD51-5 is the key:

require ‘json’

body =<<JSON_DATA
{“BD51-5”:276611,“TOTAL”:1459071,“BD51-10”:1182460}
JSON_DATA

my_hash = JSON.parse(body)
p my_hash

–output:–
{“BD51-5”=>276611, “TOTAL”=>1459071, “BD51-10”=>1182460}

if my_hash.has_key?(“BD51-5”)
puts “Yes”
end

–output:–
Yes

Ferdous ara wrote in post #1077222:

Hi
I am trying to read about json output , buts its getting little bit of
hard

url = “http://xxxxxxx:111/v1=rebuild
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
puts data =:::::::::::
{“BD51-5”:276611,“TOTAL”:1459071,“BD51-10”:1182460}
result = JSON.parse(data)
puts result =:::::::::::::: BD51-101182460TOTAL1459071BD51-5276611

puts result.inspect

will make it a lot clearer. You have a Hash there, but Hash#to_s in ruby
1.8.x just concatenates the keys and values which makes it hard to read.

Am 23.09.2012 23:53, schrieb Ferdous ara:

puts data =:::::::::::
{“BD51-5”:276611,“TOTAL”:1459071,“BD51-10”:1182460}
result = JSON.parse(data)
puts result =:::::::::::::: BD51-101182460TOTAL1459071BD51-5276611

Basically,

I want to see, if the output of “result” or “data” has BD51-5 or not
. ( or the search could be, if BD51-11 is there or not )

in case you do not know beforehand which exact key to look for,
you could first extract all keys with Hash#keys and then
select from the resulting array of keys those that match:

1.9.3p194 :001 > data = {‘BD51-5’ => 611, ‘TOTAL’ => 1459071, ‘BD51-10’
=> 1182460}
=> {“BD51-5”=>611, “TOTAL”=>1459071, “BD51-10”=>1182460}
1.9.3p194 :002 > hosts = data.keys.select {|key| key =~ /BD/ }
=> [“BD51-5”, “BD51-10”]

Am 23.09.2012 23:53, schrieb Ferdous ara:

puts data =:::::::::::
{“BD51-5”:276611,“TOTAL”:1459071,“BD51-10”:1182460}
result = JSON.parse(data)
puts result =:::::::::::::: BD51-101182460TOTAL1459071BD51-5276611

Basically,

I want to see, if the output of “result” or “data” has BD51-5 or not
. ( or the search could be, if BD51-11 is there or not )

in case you do not know beforehand which exact key to look for,
you could first extract all keys with Hash#keys and then
select from the resulting array of keys those that match:

1.9.3p194 :001 > data = {‘BD51-5’ => 611, ‘TOTAL’ => 1459071, ‘BD51-10’
=> 1182460}
=> {“BD51-5”=>611, “TOTAL”=>1459071, “BD51-10”=>1182460}
1.9.3p194 :002 > hosts = data.keys.select {|key| key =~ /BD/ }
=> [“BD51-5”, “BD51-10”]