Radio TukTuk
Radio TukTuk

Reputation: 273

Issue in parsing Json response in ruby

  @response = Typhoeus::Request.get(FOUR_SQUARE_API_SERVER_ADDRESS+'search?ll=' + current_user.altitude.to_s + "&query="+ params[:query] + FOUR_SQUARE_API_ACESS_CODE)
   @venues = ActiveSupport::JSON.decode(@response.body)
   @venues['response']['groups'][0]['items'].each do |venue|
     venue['name']  //working
      venue['name']['location'][0]['address']  //issues
     venue['name']['categories'][0]['id']  //issues
   end 

Please check inline comments for issues.

enter image description here

Upvotes: 0

Views: 793

Answers (1)

mu is too short
mu is too short

Reputation: 434615

First of all, the venue['name'] is a scalar, not an array; secondly, venue['location'] (which I think you're trying to access) is not encoded as an array, that's just an object:

location: {
    address: "...',
    city: "...",
    // ...
}

So here you want:

venue['location']

Then, your venue['name']['categories'][0]['id'] will fail because, again, venue['name'] is a scalar; for this one, you want:

venue['categories'][0]['id']

Upvotes: 2

Related Questions