Edrix89
Edrix89

Reputation: 97

How to get data from nested json with python?

I am trying to get some data from this api response. Trying to print the amount of kills but I don't understand how. Because after "stats" there are a lot of "metadata" keys.

This is the code

import requests 
import json
import sys

url = 'https://cod-api.tracker.gg/v1/standard/bo4/profile/1/Edr1X'
headers = {'secret'}
 r = requests.get(url, headers=headers)

print(r.text)

#Get Kills amount
data = r.text


#load the json to a string
 resp = json.loads(data)

#get the stats
print (resp['data']['stats'])

#get the amount of kills how?
print (resp['data']['stats']['metadata']['kills'])

This is the response from print r.text

{
    "data": {
        "id": "9d0cd2c0-9408-4925-908d-5fcf0dfeb0b9",
        "type": "player",
        "metadata": {
            "statsCategoryOrder": [
                "levels",
                "combat",
                "game",
                "bullets",
                "ekia-enemy-killed-in-action",
                "objective",
                "team-play",
                "extra"
            ],
            "platformId": 1,
            "platformUserHandle": "Edr1X",
            "accountId": "9d0cd2c0-9408-4925-908d-5fcf0dfeb0b9",
            "cacheExpireDate": "11/1/2018 7:21:38 PM"
        },
        "stats": [
            {
                "metadata": {
                    "key": "level",
                    "name": "Level",
                    "categoryKey": "levels",
                    "categoryName": "Levels",
                    "isReversed": false,
                    "iconUrl": "https://cod-cdn.tracker.gg/assets/ranks/rank_13.png"
                },
                "value": 13,
                "percentile": 83,
                "displayValue": "13"
            },
            {
                "metadata": {
                    "key": "KDRatio",
                    "name": "KD Ratio",
                    "categoryKey": "combat",
                    "categoryName": "Combat",
                    "isReversed": false
                },
                "value": 0.78,
                "percentile": 85,
                "displayValue": "0.78",
                "displayRank": ""
            },
            {
                "metadata": {
                    "key": "Kills",
                    "name": "Kills",
                    "categoryKey": "combat",
                    "categoryName": "Combat",
                    "isReversed": false
                },
                "value": 161,
                "percentile": 86,
                "displayValue": "161",
                "displayRank": ""
            },
            {
                "metadata": {
                    "key": "Deaths",
                    "name": "Deaths",
                    "categoryKey": "combat",
                    "categoryName": "Combat",
                    "isReversed": false
                },
                "value": 206,
                "percentile": 83,
                "displayValue": "206",
                "displayRank": ""
            },
            {
                "metadata": {
                    "key": "Assists",
                    "name": "Assists",
                    "categoryKey": "combat",
                    "categoryName": "Combat",
                    "isReversed": false
                },
                "value": 61,
                "percentile": 81,
                "displayValue": "61",
                "displayRank": ""
            },
            {
                "metadata": {
                    "key": "Melee",
                    "name": "Melee",
                    "categoryKey": "combat",
                    "categoryName": "Combat",
                    "isReversed": false
                },
                "value": 4,
                "percentile": 32,
                "displayValue": "4",
                "displayRank": ""
            },
            {
                "metadata": {
                    "key": "Suicides",
                    "name": "Suicides",
                    "categoryKey": "combat",
                    "categoryName": "Combat",
                    "isReversed": false
                },
                "value": 0,
                "displayValue": "0",
                "displayRank": ""
            },       

        ]
    }
}

How can I get the value of kills? Must I iterate over "metadata"? with a for loop?

Upvotes: 0

Views: 120

Answers (1)

pythomatic
pythomatic

Reputation: 657

print([metadata['value'] for metadata in resp['data']['stats'] if metadata['metadata']['key'] == 'Kills'])

Use a list comprehension to go through each metadata to see if its key is 'Kills'.

Alternatively, if kills is always the same index of the list, you can just request that list location.

Here's the test I wrote to verify your information:

def test_get_kills(self):
    import json
    resp = json.loads(resp)
    kills = [metadata['value'] for metadata in resp['data']['stats'] if metadata['metadata']['key'] == 'Kills']
    self.assertEqual(161, next(kills))

If list comprehension isn't your thing (it should be), then the equivalent in loops would be:

for metadata in resp['data']['stats']:
    if metadata['metadata']['key'] == 'Kills':
        kills = metadata['value']

Upvotes: 1

Related Questions