I am parsing JSON from an API with the following code:
def json_test
content = open("API URL").read
json = JSON.parse(content)
json.each do |a|
puts a["first_name"]
end
end
The reason I'm using each is because the API request will return an array of hashes for multiple users, like this:
[{
"id": "1",
"first_name": "John"
},
{
"id": "2",
"first_name": "Bob"
}]
However, the API will return just a hash if the request only returns a single user, like thus:
{
"id": "2",
"first_name": "Bob"
}
This is where I'm getting the error message: can't convert String into Integer (TypeError)
I've been searching for a good way to be able to parse when it's not returning an array but just a hash and I'm stumped. Any pointers?