'I want to call the same key in all arrays from a JSON that I'm pulling from an API. Something like API_Source["data"][1..4]. How can I do this?
Here is my current code where I'm using the API to seed a database.
@games.each do |game|
Game.create(
local_team: @games["data"][0]["visitorTeam"]["data"]["name"],
away_team: @games["data"][0]["localTeam"]["data"]["name"]);
end
I can go through, one by one, and just increase the "[0]" one line at a time, but that would make for a wall of syntax.
How can I call something like "[0, 1, 2, 3, 4]", etc?
Here's what @games looks like in JSON
{
"data": [
{
"localTeam": {
"data": {
"name": "Celtic",
}
},
"visitorTeam": {
"data": {
"name": "Rangers",
}
}
},
{
"localTeam": {
"data": {
"name": "Rangers",
}
},
"visitorTeam": {
"data": {
"name": "Hearts",
}
}
},
This continues for another 10 matches. I want to call around a few hundred so you can imagine why I'd want to streamline this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
