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
on 2012-09-23 23:49
on 2012-09-24 00:30
> {"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
on 2012-09-24 09:56
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.
on 2012-09-24 15:17
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"]
on 2012-09-24 15:34
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"]
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.