Jason Attwood
Jason Attwood

Reputation: 59

Parsing and then reading specific values of JSON / HTTParty::Response

I want to be able to retrieve the tier and division from this code, however when using the response object from HTTParty and doing res[0]["#{id}"]["tier"] it comes up with "cannot implicitly convert string to integer", which means it expects an integer, but I don't know where

This is the response I get (I'm doing it in a loop which is why I'm putting in the ID with "#{id}")

{"37714607": [
   {
      "queue": "RANKED_SOLO_5x5",
      "name": "Diana's Patriots",
      "entries": [{
         "leaguePoints": 32,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "IV",
         "isInactive": false,
         "isVeteran": false,
         "losses": 65,
         "playerOrTeamName": "Wicked7000",
         "playerOrTeamId": "37714607",
         "wins": 59
      }],
      "tier": "GOLD"
   },
   {
      "queue": "RANKED_TEAM_5x5",
      "name": "Nasus's Justicars",
      "entries": [{
         "leaguePoints": 81,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "V",
         "isInactive": false,
         "isVeteran": false,
         "losses": 73,
         "playerOrTeamName": "Pink Fedoras",
         "playerOrTeamId": "TEAM-5ffedf90-45ba-11e4-9e4b-c81f66db8bc5",
         "wins": 73
      }],
      "tier": "SILVER"
   },
   {
      "queue": "RANKED_TEAM_3x3",
      "name": "Cassiopeia's Marksmen",
      "entries": [{
         "leaguePoints": 0,
         "isFreshBlood": false,
         "isHotStreak": true,
         "division": "I",
         "isInactive": false,
         "isVeteran": false,
         "losses": 3,
         "playerOrTeamName": "The Booty Brothers",
         "playerOrTeamId": "TEAM-53a65b60-ff2d-11e4-9e51-c81f66dba0e7",
         "wins": 7
      }],
      "tier": "BRONZE"
   }
]}

Upvotes: 1

Views: 134

Answers (2)

Rajarshi Das
Rajarshi Das

Reputation: 12350

As your json something like below

{"37714607": [
   {
      "queue": "RANKED_SOLO_5x5",
      "name": "Diana's Patriots",
      "entries": [{
         "leaguePoints": 32,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "IV",
         "isInactive": false,
         "isVeteran": false,
         "losses": 65,
         "playerOrTeamName": "Wicked7000",
         "playerOrTeamId": "37714607",
         "wins": 59
      }],
      "tier": "GOLD"
   },

so it will first id = "37714607" then an array start([) the array contains hashes so first hash has "tier" key

so it should be

 tiers = []
 res["#{id}"].each do |result| #id = 37714607 
   tiers << result["tier"]
 end

Upvotes: 3

EugZol
EugZol

Reputation: 6555

Seems like you need to do res[id.to_s][0]["tier"] instead – first take the root key, then first element (you did it vice versa).

Upvotes: 1

Related Questions