Yooooomi Yooooomi - 5 months ago 9
Python Question

How to parse this code?

I'm stuck since this morning on how to get the 'Tier' of this json answer from here. I have been trying this:

url = #given
response = requests.get(url)
data = json.loads(response.text)

for ids in data:
print(ids['tier'])


And it keeps telling me this :
print(ids['tier']) TypeError: string indices must be integers

Answer

You are looking for the following:

import requests
import json

url = 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/266302,29338851,44112476,24693443,42900539,32556822,25575027,27677126,24648065,19083702/entry?api_key=2c0543ab-aeca-476d-8276-a5c23748898c'
response = requests.get(url)
data = json.loads(response.text)

for ids in data:
    print(data[ids][0]['tier'])

Output

PLATINUM
SILVER
SILVER
Comments