xamenrax
xamenrax

Reputation: 1744

Rails parse HTTPparty json response

I'm making request like this:

response = HTTParty.get("http://vimeo.com/api/v2/video/#{ id }.json")

And my response.body does look like this:

[{"id"=>44747277, "title"=>"ALPINIST feat. Chris O'Hara of PATSY O'HARA", ...}]

When I'm trying response.body["id"] it returns id, so, how I can handle this json correctly?

Upvotes: 0

Views: 236

Answers (2)

zeantsoi
zeantsoi

Reputation: 26193

response is an HTTParty::Response object. response.body returns the body in a string format. To get it as an array, run the HTTParty parsed_response method on your object. That will provide an array that you can operate on in the fashion depicted in the question.

response.parsed_response[0]['id'] #=> 44747277

Upvotes: 3

Luís Ramalho
Luís Ramalho

Reputation: 10208

Your JSON is returning an array, thus to get the first element's id you should do something like:

response[0]['id']

Upvotes: 0

Related Questions