tags:

views:

62

answers:

2

Here is what it looks like:

{
  "groups" => [
    { "venues" => [
      { "city"     => "Madrid",
        "address"  => "Camino de Perales, s/n",
        "name"     => "Caja Mágica",
        "stats"    => {"herenow"=>"0"},
        "geolong"  => -3.6894333,
        "primarycategory" => {
          "iconurl"      => "http://foursquare.com/img/categories/arts_entertainment/stadium.png",
          "fullpathname" => "Arts & Entertainment:Stadium",
          "nodename"     => "Stadium",
          "id"           => 78989 },
        "geolat"   => 40.375045,
        "id"       => 2492239,
        "distance" => 0,
        "state"    => "Spain" }],
      "type"   => "Matching Places"}]
}

Big and ugly... I just want to grab the id out. How would I go about doing this?

+2  A: 

h = { "groups" => ......... }

The two ids are:

h["groups"][0]["venues"][0]["primarycategory"]["id"]
h["groups"][0]["venues"][0]["id"]
Sudhanshu
or `h["groups"][0]["venues"][0]["primarycategory"]["id"]` depending on what id he meant
jigfox
Yeah, I just realized there was another one there. :)
Sudhanshu
wanted the venue id, this works great
jtmkrueger
A: 

If the hash stores one id:(assuming the value is stored in a variable called hash)

hash["groups"][0]["venues"][0]["primarycategory"]["id"] rescue nil

If the hash stores multiple ids then:

ids = Array(hash["groups"]).map do |g|
  Array(g["venues"]).map do |v|
    v["primarycategory"]["id"] rescue nil
  end.compact
end.flatten

The ids holds the array of id's.

KandadaBoggu