Eric.L Eric.L - 14 days ago 9
JSON Question

How do I call specific json data?

i'm currently working on a project that involves getting data from RiotGames API and would apprciate some help in this problem I can't seem to solve.

The code:

import requests
r = requests.get(url="https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/7250/ranked?season=SEASON2016&api_key=ABC420")
json_data = r.json()
get_summoner_data = (json_data["champions"])
print(get_summoner_data)


And this is the response

{"summonerId":7250,"modifyDate":1479970436000,"champions":[{"id":111,"stats":{"totalSessionsPlayed":2,"totalSessionsLost":2,"totalSessionsWon":0,"totalChampionKills":8,"totalDamageDealt":211063,"totalDamageTaken":42772,"mostChampionKillsPerSession":7,"totalMinionKills":318,"totalDoubleKills":1,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":15,"totalGoldEarned":21574,"mostSpellsCast":0,"totalTurretsKilled":1,"totalPhysicalDamageDealt":33376,"totalMagicDamageDealt":174433,"totalFirstBlood":0,"totalAssists":11,"maxChampionsKilled":7,"maxNumDeaths":8}},{"id":110,"stats":{"totalSessionsPlayed":3,"totalSessionsLost":3,"totalSessionsWon":0,"totalChampionKills":12,"totalDamageDealt":463303,"totalDamageTaken":49704,"mostChampionKillsPerSession":8,"totalMinionKills":675,"totalDoubleKills":0,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":15,"totalGoldEarned":35203,"mostSpellsCast":0,"totalTurretsKilled":2,"totalPhysicalDamageDealt":438295,"totalMagicDamageDealt":22391,"totalFirstBlood":0,"totalAssists":28,"maxChampionsKilled":8,"maxNumDeaths":6}}


So the respone is basically data of the players performance with different champions, in this case defined by their id's.

But let's say someone is playing the champion with the id 111, now I want to get all data connected to that id and then be able to print out certain parts ex: MaxDeaths and MaxGold.

But if no data for that id is found I would have a message like "Data not found".

As you can see here there is data for the champion-id 111, but how do I call to get only that data?

Side note for better understading:
A player is in a game with 10 other players, 5 on each team. Every single person is playing different champions. I want to see if the player has any prior experience with that champion (All that data), and will therefore check if the champion-id is in the summary. If it is, I will use that data to make some sort of performance analyse.

Answer
for i in json_data["champions"]:
  if i["id"]=="111":
    print("found")
    print(i["stats"]["maxNumDeaths"])
else:
  print("not found")