Jason Attwood
Jason Attwood

Reputation: 59

How to iterate over this Json data in rails?

I'm trying to Iterate over this Json data I want to get the Items out of "data I tried doing

@Response =HTTParty.get(("http://ddragon.leagueoflegends.com/cdn/5.15.1/data/en_US/item.json"))
puts @Response["data"].each do |item|
    puts item
end

Also I want to be able to iterate them without having to know the IDS prior for example the first item is "1001" I don't want to have to enter those manually

but this just says that it can't find anything with '[]' for Nil:NilClass Also before anyone mentions the JSON below doesn't finish because I only took a sample there are a lot more Items and if I were to paste it , it would easily be over 1000 Lines.

"type":"item",
"version":"5.15.1",
"basic":{},
"data":{
        "1001":{
                "name":"Boots of Speed",
                "group":"BootsNormal",
                "description":"<groupLimit>Limited to 1.</groupLimit><br><br>                                                 <unique>UNIQUE Passive - Enhanced Movement:</unique> +25 Movement Speed<br><br>                 <i>(Unique Passives with the same name don't stack.)</i>",
                "colloq":";",
                "plaintext":"Slightly increases Movement Speed",
                "into":[
                        "3006",
                        "3047",
                        "3020",
                        "3158",
                        "3111",
                        "3117",
                        "3009"
                ],
                "image":{
                        "full":"1001.png",
                        "sprite":"item0.png",
                        "group":"item",
                        "x":0,
                        "y":0,
                        "w":48,
                        "h":48
                },
                "gold":{
                        "base":325,
                        "purchasable":true,
                        "total":325,
                        "sell":227
                },
                "tags":[
                        "Boots"
                ],
                "stats":{
                        "FlatMovementSpeedMod":25.0
                }
        },
        "1004":{
                "name":"Faerie Charm",
                "description":"<stats><mana>+25% Base Mana Regen </mana></stats>",
                "colloq":";",
                "plaintext":"Slightly increases Mana Regen",
                "into":[
                        "3028",
                        "3070",
                        "3073",
                        "3114"
                ],
                "image":{
                        "full":"1004.png",
                        "sprite":"item0.png",
                        "group":"item",
                        "x":48,
                        "y":0,
                        "w":48,
                        "h":48
                },
                "gold":{
                        "base":180,
                        "purchasable":true,
                        "total":180,
                        "sell":126
                },
                "tags":[
                        "ManaRegen"
                ],
                "stats":{
                }
        },

Upvotes: 1

Views: 2691

Answers (2)

Gautam
Gautam

Reputation: 1812

I had done something like this before, maybe that can help you here is the link - Reading Json using Ruby

Upvotes: 0

Tamer Shlash
Tamer Shlash

Reputation: 9523

Four problems:

  1. you are not parsing the response body with JSON.parse
  2. you are using a constant name (starting with capital letter) as a variable name (@Response), you shouldn't.
  3. unnecessary puts on iteration.
  4. The value of data is not an array, but rather another hash. So you should iterate over it as key/value pairs.

Solution:

@response = JSON.parse(HTTParty.get(your_url).body)
@response["data"].each do |key, value|
  puts key
  puts value
end

Upvotes: 3

Related Questions