Access hash key value in array of hashes

Virtueel

New Member
I'm trying to obtain the \[code\]:visits\[/code\] value. I'm using this code to access the value\[code\]puts "#{my_hash[0]['metrics'][0][:visits]}"\[/code\]and getting this error:\[code\]undefined method `[]'\[/code\]Edited: Here is the full output of \[code\]puts my_hash\[/code\]\[code\]{"xml"=>"<entry><id>ID_DATA</id><updated>2013-04-01T18:10:36.599Z</updated><title type=\"text\">ga:month=01 | ga:year=2013</title><link rel=\"alternate\" type=\"text/html\" href=http://stackoverflow.com/"http://www.google.com/analytics\" /><dxp:dimension name=\"ga:month\" value=http://stackoverflow.com/"01\" /><dxp:dimension name=\"ga:year\" value=http://stackoverflow.com/"2013\" /><dxp:metric name=\"ga:visits\" type=\"integer\" value=http://stackoverflow.com/"26248\" /><dxp:metric name=\"ga:bounces\" type=\"integer\" value=http://stackoverflow.com/"17174\" /></entry>", "id"=>"https://www.googleapis.com/analytics/v2.4/data?ids=ga:45872215&ga:month=01&ga:year=2013&start-date=2013-01-01&end-date=2013-03-28", "updated"=>Mon, 01 Apr 2013 18:10:36 +0000, "title"=>"ga:month=01 | ga:year=2013", "dimensions"=>[{:month=>"01"}, {:year=>"2013"}], "metrics"=>[{:visits=>26248.0}, {:bounces=>17174.0}]}{"xml"=>"<entry><id>ID_DATA</id><updated>2013-04-01T18:10:36.599Z</updated><title type=\"text\">ga:month=02 | ga:year=2013</title><link rel=\"alternate\" type=\"text/html\" href=http://stackoverflow.com/"http://www.google.com/analytics\" /><dxp:dimension name=\"ga:month\" value=http://stackoverflow.com/"02\" /><dxp:dimension name=\"ga:year\" value=http://stackoverflow.com/"2013\" /><dxp:metric name=\"ga:visits\" type=\"integer\" value=http://stackoverflow.com/"24808\" /><dxp:metric name=\"ga:bounces\" type=\"integer\" value=http://stackoverflow.com/"16716\" /></entry>", "id"=>"https://www.googleapis.com/analytics/v2.4/data?ids=ga:45872215&ga:month=02&ga:year=2013&start-date=2013-01-01&end-date=2013-03-28", "updated"=>Mon, 01 Apr 2013 18:10:36 +0000, "title"=>"ga:month=02 | ga:year=2013", "dimensions"=>[{:month=>"02"}, {:year=>"2013"}], "metrics"=>[{:visits=>24808.0}, {:bounces=>16716.0}]}{"xml"=>"<entry><id>ID_DATA</id><updated>2013-04-01T18:10:36.599Z</updated><title type=\"text\">ga:month=03 | ga:year=2013</title><link rel=\"alternate\" type=\"text/html\" href=http://stackoverflow.com/"http://www.google.com/analytics\" /><dxp:dimension name=\"ga:month\" value=http://stackoverflow.com/"03\" /><dxp:dimension name=\"ga:year\" value=http://stackoverflow.com/"2013\" /><dxp:metric name=\"ga:visits\" type=\"integer\" value=http://stackoverflow.com/"23556\" /><dxp:metric name=\"ga:bounces\" type=\"integer\" value=http://stackoverflow.com/"16261\" /></entry>", "id"=>"https://www.googleapis.com/analytics/v2.4/data?ids=ga:45872215&ga:month=03&ga:year=2013&start-date=2013-01-01&end-date=2013-03-28", "updated"=>Mon, 01 Apr 2013 18:10:36 +0000, "title"=>"ga:month=03 | ga:year=2013", "dimensions"=>[{:month=>"03"}, {:year=>"2013"}], "metrics"=>[{:visits=>23556.0}, {:bounces=>16261.0}]}\[/code\]I'm using the Gattica \[code\]to_hash\[/code\] method. https://github.com/chrisle/gattica\[code\]my_hash = data.to_h['points']\[/code\]
 
Back
Top