REST Countries v1

FREE
By apilayer | Updated 4 days ago | Data
Popularity

0.5 / 10

Latency

44ms

Service Level

100%

Health Check

N/A

Back to All Discussions

Pulling API data / Rails / Hash class?

Rapid account: Dbeslic
dbeslic
8 years ago

Hi. I am pulling the country data in JSON and then parsing it into Ruby on Rails. With other API’s , the parsed Response Body is returned as a ruby Hash, between squiggly brackets {data}. Yet the Response Body I get from this API doesn’t seem to be a hash. Example for Mexico is below. The squiggly brackets have square brackets [body] around them, yet it it doesn’t seem to be an Array either.

I tried running both Hash methods and Array methods on the parsed body, and simply got a Nil Class error: undefined method `[]’ for nil:NilClass

Any idea how I might be able to resolve? I would like to get the parsed response body, and then run several Hash methods on it in my rails program.

Thanks!
D

[{
“name”=>“Mexico”,
“capital”=>“Mexico City”,
“altSpellings”=>[“MX”, “Mexicanos”, “United Mexican States”, “Estados Unidos Mexicanos”],
“relevance”=>“1.5”, “region”=>“Americas”, “subregion”=>“Central America”,
“translations”=>{“de”=>“Mexiko”, “es”=>“México”, “fr”=>“Mexique”, “ja”=>"???",
“it”=>“Messico”}, “population”=>121740000, “latlng”=>[23.0, -102.0], “demonym”=>“Mexican”,
“area”=>1964375.0, “gini”=>47.0, “timezones”=>[“UTC?08:00”, “UTC?07:00”, “UTC?06:00”],
“borders”=>[“BLZ”, “GTM”, “USA”], “nativeName”=>“México”, “callingCodes”=>[“52”],
“topLevelDomain”=>[".mx"],
“alpha2Code”=>“MX”,
“alpha3Code”=>“MEX”,
“currencies”=>[“MXN”],
“languages”=>[“es”]
}]

Rapid account: Fayder
fayder Commented 8 years ago

Hello guys. I’m sorry I never answered this question. I probably tried to figure out the issue but I couldn’t. This response that you post there is not the response you get from the API. I don’t know what Ruby on Rails is doing to it.

Sorry for not being helpful on this one 😦

Fayder

Rapid account: Orangejava
orangejava Commented 8 years ago

aaa

Join in the discussion - add comment below:

Login / Signup to post new comments