I have the following hash:
{
"groups" => [
{
"type" => "Nearby",
"venues" => [
{
"id" => 4450132,
"name" => "Position2",
"address" => "Domlur",
"city" => "Bangalore",
"state" => "Karnataka/India",
"zip" => "560037",
"verified" => false,
"geolat" => 12.9566921,
"geolong" => 77.6407258,
"stats" => {
"herenow" => "0"
},
"twitter" => "position2",
"hasTodo" => "false",
"distance" => 0
},...
I want to iterate through it and find all the ‘name’ attributes. My code looks like:
response["groups"]["Nearby"]["venues"].each do |key|
logger.debug key['name']
end
But I keep on getting error:
TypeError (can't convert String into Integer):
I am on ruby 1.9.
If I clean up the hash so it’s properly balanced:
I can iterate over the hash finding the
'name'keys using:Because the data is nested, the resulting extracted data will be nested. To flatten it use
flatten: