Lumbee Lumbee - 1 month ago 6
JSON Question

Process json response in ruby

I am attempting to consume data from an API. Here is the code I have that currently works:

...connection info here
webinars = response.parsed_response["webinars"]

webinars.each do |w|
puts w
end


This gives me back an array like:

{"uuid"=>"2342342==", "id"=>285433, "host_id"=>"3423",
"topic"=>"Requirement Changes", "agenda"=>"", "status"=>0,
"option_start_type"=>"video", "option_host_video"=>true, "option_panelist_video"=>true,
"option_audio"=>"both", "option_enforce_login"=>false, "type"=>5,
"start_time"=>"2016-10-24T14:00:00Z", "duration"=>90, "timezone"=>"America/New_York",
"start_url"=>"blah.com",
"join_url"=>"blam.com", "created_at"=>"2016-09-22T13:31:09Z"},
{"uuid"=>"2342343==", "id"=>285434, "host_id"=>"3423",
"topic"=>"Requirement Changes", "agenda"=>"", "status"=>0,
"option_start_type"=>"video", "option_host_video"=>true, "option_panelist_video"=>true,
"option_audio"=>"both", "option_enforce_login"=>false, "type"=>5,
"start_time"=>"2016-10-24T14:00:00Z", "duration"=>90, "timezone"=>"America/New_York",
"start_url"=>"blah.com",
"join_url"=>"blam.com", "created_at"=>"2016-09-22T13:31:09Z"}


I would like to access each of attributes in each webinar like...

webinars = response.parsed_response["webinars"]

webinars.each do |w|
puts w.id
end


How should I be processing this so I can call attributes like this?

Answer

You can just do w["id"] instead of w.id

If your map would have been composed of {uuid: "2342342==", id: 285433, host_id: "3423"} you could have called w.id on it.

Comments