Eric Eric - 23 days ago 7
JSON Question

Python, taking specific data from JSON

I'm playing around with the league of legends api, anyway I made this very simple program.

import requests
region = input("Enter a region ")
summonerName = input("Enter Summoner Name ")
apikey = input("Enter APIKey ")
r = requests.get(url="https://" + region + ".api.pvp.net/api/lol/" + region + "/v1.4/summoner/by-name/" + summonerName + "?api_key=" + apikey)
print(r.json())


and this is what it returns.

{'hiimeric': {'revisionDate': 1478543641000, 'name': 'Hi Im Eric', 'id': 36843151, 'profileIconId': 13, 'summonerLevel': 30}}


so my question is now, how could I for example get it to only post 'name' or only 'name' and 'profileIconId'? Thanks!

Answer

A JSON object is actually just a combination of dictionaries and lists. Hence, you can print the name and profileIconId with the following:

print(r.json()['hiimeric']['name'])
print(r.json()['hiimeric']['profileIconId'])
Comments