0

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?

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

6 Comments

Traceback (most recent call last): File "getInfo.py", line 28, in <module> print([metadata['kills'] for metadata in resp['data']['stats'] if metadata['key'] == 'Kills']) File "getInfo.py", line 28, in <listcomp> print([metadata['kills'] for metadata in resp['data']['stats'] if metadata['key'] == 'Kills']) KeyError: 'key'
[161.0] is the output. Could you be kind to explain what you did?
Nice answer plus 1+
List comprehension is what you're looking for. Basically what you wanted was the value for a key in a list of dicts. List comprehension allows you to look through each element of the list, in order to create a new one. It also allows conditionals, which is what we needed for finding the correct dict in the list. I'll post an equivalent loop in a sec.
Updated answer to include a version without list comprehension.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.