Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails accessing keys in hashes from JSON data

I have a hash from a JSON object. It looks like this:

JSON = {"kind"=>"calendar#freeBusy", "timeMin"=>"2012-02-19T19:35:00.000Z", "timeMax"=>"2012-02-19T19:40:00.000Z", "calendars"=>{"[email protected]"=>{"busy"=>[{"start"=>"2012-02-19T19:35:00Z", "end"=>"2012-02-19T19:40:00Z"}]}}}

I want to check if there's any children of busy (a child of calendars).

I can access the children of calendars by JSON["calendars"]

That returns:

{"[email protected]"=>{"busy"=>[{"start"=>"2012-02-19T19:35:00Z", "end"=>"2012-02-19T19:40:00Z"}]}}

but JSON["calendars"]["busy"] (which is how I think you're meant to access child elements?) returns nil.

How do I get inside the "busy" child?

I created the hash using JSON.parse to a Faraday request.

like image 276
William Stock Avatar asked Dec 03 '25 02:12

William Stock


1 Answers

"busy" is nested under "[email protected]" in your case, so you need to include this as well: JSON["calendars"]["[email protected]"]["busy"].If you want access this attribute for all calendars, you'll have to loop:

JSON["calendars"].each do |key, value|
  # working with value["busy"]...
end
like image 142
Ineu Avatar answered Dec 05 '25 15:12

Ineu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!