Search code examples
ruby-on-railsjsonapiparsingzillow

Zillow API parse the JSON Ruby on Rails 4


I'm hitting Zillow's seach API at http://www.zillow.com/webservice/GetSearchResults.htm and I've converted the code to JSON using HTTParty. How would I pull out a specific value, such as "address" or "city"?

I was trying something like response.price, but that was wrong. Then I was trying response[0] and response['city'], and things like that...

[1] pry(Zillow)> response
=> {"searchresults"=>
  {"request"=>{"address"=>"7 Clive St UNIT 2", "citystatezip"=>"02130"},
   "message"=>{"text"=>"Request successfully processed", "code"=>"0"},
   "response"=>
    {"results"=>
      {"result"=>
        {"zpid"=>"120921393",
         "links"=>
          {"homedetails"=>
            "http://www.zillow.com/homedetails/7-Clive-St-UNIT-2-Jamaica-Plain-M
           "graphsanddata"=>
            "http://www.zillow.com/homedetails/7-Clive-St-UNIT-2-Jamaica-Plain-M
           "mapthishome"=>"http://www.zillow.com/homes/120921393_zpid/",
           "comparables"=>"http://www.zillow.com/homes/comps/120921393_zpid/"},
         "address"=>
          {"street"=>"7 Clive St UNIT 2",
           "zipcode"=>"02130",
           "city"=>"Jamaica Plain",
           "state"=>"MA",
           "latitude"=>"42.317562",
           "longitude"=>"-71.108245"},
         "zestimate"=>
          {"amount"=>{"__content__"=>"500537", "currency"=>"USD"},
           "last_updated"=>"04/20/2014",
           "oneWeekChange"=>{"deprecated"=>"true"},
           "valueChange"=>nil,
           "valuationRange"=>
            {"low"=>{"__content__"=>"475510", "currency"=>"USD"},
             "high"=>{"__content__"=>"525564", "currency"=>"USD"}},
           "percentile"=>"0"},
         "localRealEstate"=>
          {"region"=>
            {"links"=>
              {"overview"=>
                "http://www.zillow.com/local-info/MA-Boston/Jamaica-Plain/r_1547
               "forSaleByOwner"=>
                "http://www.zillow.com/jamaica-plain-boston-ma/fsbo/",
               "forSale"=>"http://www.zillow.com/jamaica-plain-boston-ma/"},
             "id"=>"154795",
             "type"=>"neighborhood",
             "name"=>"Jamaica Plain"}}}}},
   "schemaLocation"=>
    "http://www.zillow.com/static/xsd/SearchResults.xsd http://www.zillowstatic.
(END)

Solution

  • Great, I figured it out. Here's an example:

    response["searchresults"]["response"]["results"]["result"]["zestimate"]["amount"]["__content__"]
    

    Each set of square-brackets is accessing further inside of the nesting. Hopefully they don't change the formatting!