Ryan
Ryan

Reputation: 1

How to output specific values in this json file?

So I have this python code:

import requests
import json

url = requests.get('https://ow-api.com/v1/stats/pc/eu/rGInfini-2810/profile')
data = [url.json()]

with open('sr.json', 'w') as output:
    json.dump(data, output, indent=4)

with open('sr.json', 'r') as file:
    data = json.load(file)
    for rank in data['ratings']:
        print('Role: ' + rank['role'])
        print('SR: ' + rank['level'])

but I cannot get it to output the rankings that are in the json file. How would I go about doing this?

The JSON data looks like this:

[
    {
        "competitiveStats": {
            "awards": {
                "cards": 0,
                "medals": 28,
                "medalsBronze": 6,
                "medalsSilver": 17,
                "medalsGold": 5
            },
            "games": {
                "played": 14,
                "won": 4
            }
        },
        "endorsement": 3,
        "endorsementIcon": "https://static.playoverwatch.com/svg/icons/endorsement-frames-3c9292c49d.svg#_3",
        "gamesWon": 856,
        "icon": "https://d15f34w2p8l1cc.cloudfront.net/overwatch/f5dcd83834fc807c6d1452479828e57bf5a0c630a57e1f792854efc01c0610e6.png",
        "level": 11,
        "levelIcon": "https://d15f34w2p8l1cc.cloudfront.net/overwatch/69c2c1aff0db8429a980bad7db76a3388003e43f0034097dc4cfa7f13c5de7d7.png",
        "name": "rGInfini#2810",
        "prestige": 3,
        "prestigeIcon": "https://d15f34w2p8l1cc.cloudfront.net/overwatch/4a2c852a16043f613b7bfac33c8536dd9f9621a3d567174cb4ad9a80e3b13102.png",
        "private": false,
        "quickPlayStats": {
            "awards": {
                "cards": 64,
                "medals": 833,
                "medalsBronze": 258,
                "medalsSilver": 310,
                "medalsGold": 265
            },
            "games": {
                "played": 0,
                "won": 134
            }
        },
        "rating": 2322,
        "ratingIcon": "https://d1u1mce87gyfbn.cloudfront.net/game/rank-icons/rank-GoldTier.png",
        "ratings": [
            {
                "level": 2329,
                "role": "damage",
                "roleIcon": "https://static.playoverwatch.com/img/pages/career/icon-offense-6267addd52.png",
                "rankIcon": "https://d1u1mce87gyfbn.cloudfront.net/game/rank-icons/rank-GoldTier.png"
            },
            {
                "level": 2316,
                "role": "support",
                "roleIcon": "https://static.playoverwatch.com/img/pages/career/icon-support-46311a4210.png",
                "rankIcon": "https://d1u1mce87gyfbn.cloudfront.net/game/rank-icons/rank-GoldTier.png"
            }
        ]
    }
]

Upvotes: 0

Views: 74

Answers (1)

Daniel
Daniel

Reputation: 42748

What is the purpose to put the json-data into a list? Leave it out, and everything works as expected.

import requests
import json

URL = 'https://ow-api.com/v1/stats/pc/eu/rGInfini-2810/profile'
response = requests.get(URL)
data = response.json()

with open('sr.json', 'w') as output:
    json.dump(data, output, indent=4)

with open('sr.json', 'r') as file:
    data = json.load(file)
    for rank in data[0]['ratings']:
        print(f"Role: {rank['role']}")
        print(f"SR: {rank['level']}")

Output is:

Role: damage
SR: 2329
Role: support
SR: 2316

Upvotes: 1

Related Questions