Transformation of JSON file per field - python

I have a JSON file in this format :
[
{"itemId": "1", "score": 0.2, "userId": "1", "rank": 1},
{"itemId": "3", "score": 0.1, "userId": "1", "rank": 2},
{"itemId": "12", "score": 0.6, "userId": "2", "rank": 1},
{"itemId": "21", "score": 0.2, "userId": "2", "rank": 2},
...
]
and I would like to sort it by userId like this :
{
{
"userId": "1",
"items": [
{"itemId": "1", "score": 0.2, "rank": 1},
{"itemId": "3", "score": 0.1, "rank": 2},
...
]
},
{
"userId": "2",
"items": [
{"itemId": "12", "score": 0.6, "rank": 1},
{"itemId": "21", "score": 0.2, "rank": 2}
]
},
...
}
I tried to do it by myself with Python but I get an error that says : "TypeError: unhashable type: 'dict'".
Do you have an idea how to do it ?
Thank you !

Accumulate the items in a defaultdict(list), re-keying off the user ids:
from collections import defaultdict
data = [
{"itemId": "1", "score": 0.2, "userId": "1", "rank": 1},
{"itemId": "3", "score": 0.1, "userId": "1", "rank": 2},
{"itemId": "12", "score": 0.6, "userId": "2", "rank": 1},
{"itemId": "21", "score": 0.2, "userId": "2", "rank": 2},
]
output = defaultdict(list)
for dict_ in data:
userId = dict_.pop('userId')
output[int(userId)].append(dict_)
new_data = [{'userId': str(k), 'items': output[k]} for k in sorted(output)]

you could use pandas to load and then use groupby and sort. Then write back to json in the way you want. See below:
data = '[{"itemId": "1", "score": 0.2, "userId": "1", "rank": 1}, {"itemId": "12", "score": 0.6, "userId": "2", "rank": 1}, {"itemId": "3", "score": 0.1, "userId": "1", "rank": 2}, {"itemId": "21", "score": 0.2, "userId": "2", "rank": 2}]'
import pandas as pd
import collections
# read the json file to pandas
df = pd.read_json(data, dtype = {"itemId":object, "score": object, "userId": object, "rank":int})
# group by user id and sort them
g = df.groupby(['userId'],sort=True)
mylist = []
for k in g.groups.keys():
# create a temp dict holder
temp_dict = collections.OrderedDict()
#populate teh temp dict
temp_dict['userId'] = k
temp_dict['items'] = g['itemId','rank', 'score'].get_group(k).to_dict(orient='records')
# add the temp dict to the list
mylist.append(temp_dict)
# print as json
import json
print json.dumps(mylist,indent=4)
this will result in
[
{
"userId": "1",
"items": [
{
"itemId": "1",
"score": 0.2,
"rank": 1
},
{
"itemId": "3",
"score": 0.1,
"rank": 2
}
]
},
{
"userId": "2",
"items": [
{
"itemId": "12",
"score": 0.6000000000000001,
"rank": 1
},
{
"itemId": "21",
"score": 0.2,
"rank": 2
}
]
}
]

Related

How to get data from nested list in response.json()

There is a json response from an API request in the following schema:
[
{
"id": "1",
"variable": "x",
"unt": "%",
"results": [
{
"classification": [
{
"id": "1",
"name": "group",
"category": {
"555": "general"
}
}
],
"series": [
{
"location": {
"id": "1",
"level": {
"id": "n1",
"name": "z"
},
"name": "z"
},
"serie": {
"202001": "0.08",
"202002": "0.48",
"202003": "0.19"
}
}
]
}
]
}
]
I want to transform the data from the "serie" key into a pandas DataFrame.
I can do that explicitly:
content = val[0]["results"][0]["series"][0]["serie"]
df = pd.DataFrame(content.items())
df
0 1
0 202001 0.08
1 202002 0.48
2 202003 0.19
But if there is more than one record, that would get only the data from the first element because of the positional arguments [0].
Is there a way to retrieve that data not considering the positional arguments?
Try:
val = [
{
"id": "1",
"variable": "x",
"unt": "%",
"results": [
{
"classification": [
{"id": "1", "name": "group", "category": {"555": "general"}}
],
"series": [
{
"location": {
"id": "1",
"level": {"id": "n1", "name": "z"},
"name": "z",
},
"serie": {"202001": "0.08", "202002": "0.48", "202003": "0.19"},
}
],
}
],
},
{
"id": "2",
"variable": "x",
"unt": "%",
"results": [
{
"classification": [
{"id": "1", "name": "group", "category": {"555": "general"}}
],
"series": [
{
"location": {
"id": "1",
"level": {"id": "n1", "name": "z"},
"name": "z",
},
"serie": {"202001": "1.08", "202002": "1.48", "202003": "1.19"},
}
],
}
],
},
]
df = pd.DataFrame(
[k, v]
for i in val
for ii in i["results"]
for s in ii["series"]
for k, v in s["serie"].items()
)
print(df)
Prints:
0 1
0 202001 0.08
1 202002 0.48
2 202003 0.19
3 202001 1.08
4 202002 1.48
5 202003 1.19

Create one 'list' by userID

I want to create a list per user so i got this jsonfile:
data = [
{
"id": "1",
"price": 1,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"price": 3,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"price":8,
},
]
I'm on python and I want to have a result like
for the user with 'id':1 [1,10,10]
and for the user with "id": "2": [3,8]
so two lists corresponding to the prices according to the ids
is it possible to do that in python ?
note, in fact user id are UUID type and randomly generated.
edit: quantity was a mistake all data are price and id, sorry
collections.defaultdict to the rescue.
Assuming you really do have mixed quantitys and prices and you don't care about mixing them into the same list,
from collections import defaultdict
data = [
{
"id": "1",
"price": 1,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"quantity": 3,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"price": 8,
},
]
by_id = defaultdict(list)
for item in data:
item = item.copy() # we need to mutate the item
id = item.pop("id")
# whatever is the other value in the dict, grab that:
other_value = item.popitem()[1]
by_id[id].append(other_value)
print(dict(by_id))
The output is
{'1': [1, 10, 10], '2': [3, 8]}
If you actually only do have prices, the loop is simpler:
by_id = defaultdict(list)
for item in data:
by_id[item["id"]].append(item.get("price"))
or
by_id = defaultdict(list)
for item in data:
by_id[item["id"]].append(item["price"])
to fail fast when the price is missing.
first :
you structur data : {[]}, is not supported in python.
assume your data is :
my_json = [
{
"id": "1",
"price": 1,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"quantity": 3,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"price":8,
},
]
then you can achive with this:
results = {}
for data in my_json:
if data.get('id') not in results:
results[data.get('id')] = [data.get('price') or data.get('quantity')]
else:
results[data.get('id')].append(data.get('price') or data.get('quantity'))
print(results)
output:
{'1': [1, 10, 10], '2': [3, 8]}
Maybe like this:
data = [
{
"id": "1",
"price": 1,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"quantity": 3,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"price": 8,
}
]
result = {}
for item in data:
try:
result[item['id']].append(item.get('price'))
except KeyError:
result[item['id']] = [item.get('price')]
print(result)
Where None is put in place of the missing price for that entry, quantity key ignored.
Result:
{'1': [1, 10, 10], '2': [None, 8]}
A simple loop that enumerates your list (it's not JSON) in conjunction with setdefault() is all you need:
data = [
{
"id": "1",
"price": 1,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"price": 3,
},
{
"id": "1",
"price": 10,
},
{
"id": "2",
"price": 8,
}
]
dict_ = {}
for d in data:
dict_.setdefault(d['id'], []).append(d['price'])
print(dict_)
Output:
{'1': [1, 10, 10], '2': [3, 8]}
Note:
This will fail (KeyError) if either 'id' or 'price' is missing from the dictionaries in the list

(Python) Cant scrape data from my targeted site anymore using re, requests, and json

I'm having a problem where i can scrape data from a website by using the java pathing. I'm trying to scrape from Rocket League Tracker.
here's my code:
import requests
import re
import json
import math
def rankGetter():
trackerLink = 'https://rocketleague.tracker.network/rocket-league/profile/epic/DirectPanda/overview'
# now we have the tracker link we're going to scrape the website
# all the HTML of the site is now in result
result = requests.get(trackerLink)
# checker to make sure the user used the correct information
if result.status_code == 400:
print('profile not found')
else:
# Extract everything needed to render the current page. Data is stored as Json in the
# JavaScript variable: window.__INITIAL_STATE__={"route":{"path":"\u0 ... }};
json_string = re.search(r"window.__INITIAL_STATE__\s?=\s?(\{.*?\});", result.text).group(1)
# convert text string to structured json data
rocketleague = json.loads(json_string)
# Save structured json data to a text file that helps you orient yourself and pick
# the parts you are interested in.
with open('rocketleague_json_data.txt', 'w') as outfile:
outfile.write(json.dumps(rocketleague, indent=4, sort_keys=True))
The error is the text doc made doesn't have the ranks I want anymore.
"stats": {
"standardLeaderboardLeaders": {},
"standardLeaderboards": [],
"standardPlayers": {},
"standardTitles": {}
},
**"stats-v2": {
"segments": {},
"standardProfileMatches": {},
"standardProfileSummaries": {},
"standardProfiles": {},
"standardProfilesHistory": {},
"standardSessions": {},
"subscriptions": {}
},**
"titles": {
"currentTitle": {
"name": "Rocket League",
"platforms": [
The Ranks should be under stats-V2 but as you can see its empty now.
whats happening and how do i fix it? I was able to get ranks for a week but all the sudden it stopped working today.
Seems that the data are loaded from external URL:
import json
import requests
url = "https://api.tracker.gg/api/v2/rocket-league/standard/profile/epic/DirectPanda"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0"
}
data = requests.get(url, headers=headers).json()
print(json.dumps(data, indent=4))
Prints:
{
"data": {
"platformInfo": {
"platformSlug": "epic",
"platformUserId": null,
"platformUserHandle": "DirectPanda",
"platformUserIdentifier": "DirectPanda",
"avatarUrl": null,
"additionalParameters": null
},
"userInfo": {
"userId": null,
"isPremium": false,
"isVerified": false,
"isInfluencer": false,
"isPartner": false,
"countryCode": null,
"customAvatarUrl": null,
"customHeroUrl": null,
"socialAccounts": [],
"pageviews": 592,
"isSuspicious": null
},
"metadata": {
"lastUpdated": {
"value": "2021-04-22T17:39:42.277-04:00",
"displayValue": "2021-04-22T21:39:42.2770000+00:00"
},
"playerId": 16603481,
"currentSeason": 17
},
"segments": [
{
"type": "overview",
"attributes": {},
"metadata": {
"name": "Lifetime"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"wins": {
"rank": 30357,
"percentile": 98.3,
"displayName": "Wins",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 4985,
"displayValue": "4,985",
"displayType": "Number"
},
"goals": {
"rank": 23698,
"percentile": 98.7,
"displayName": "Goals",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 14363,
"displayValue": "14,363",
"displayType": "Number"
},
"mVPs": {
"rank": 35646,
"percentile": 98.0,
"displayName": "MVPs",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 2093,
"displayValue": "2,093",
"displayType": "Number"
},
"saves": {
"rank": 30864,
"percentile": 98.3,
"displayName": "Saves",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 9231,
"displayValue": "9,231",
"displayType": "Number"
},
"assists": {
"rank": 29228,
"percentile": 98.4,
"displayName": "Assists",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 4763,
"displayValue": "4,763",
"displayType": "Number"
},
"shots": {
"rank": 24596,
"percentile": 98.6,
"displayName": "Shots",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 29139,
"displayValue": "29,139",
"displayType": "Number"
},
"goalShotRatio": {
"rank": 1409320,
"percentile": 15.0,
"displayName": "Goal Shot Ratio",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 49.29132777377398,
"displayValue": "49.3",
"displayType": "NumberPrecision1"
},
"score": {
"rank": 28260,
"percentile": 98.4,
"displayName": "TRN Score",
"displayCategory": "General",
"category": "general",
"metadata": {},
"value": 2398222.83,
"displayValue": "2,398,222.8",
"displayType": "NumberPrecision1"
},
"seasonRewardLevel": {
"rank": null,
"percentile": 85.0,
"displayName": "Season Reward Level",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-13.png",
"rankName": "Diamond"
},
"value": 5,
"displayValue": "5",
"displayType": "Number"
},
"seasonRewardWins": {
"rank": null,
"percentile": 95.8,
"displayName": "Season Reward Wins",
"displayCategory": "General",
"category": "general",
"metadata": {},
"value": 9,
"displayValue": "9",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 0,
"season": 17
},
"metadata": {
"name": "Un-Ranked"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": null,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-0.png",
"name": "Unranked"
},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": null,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"name": "Division I"
},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": null,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": null,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "win"
},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"rating": {
"rank": 215152,
"percentile": 90.0,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1597,
"displayValue": "1,597",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 10,
"season": 17
},
"metadata": {
"name": "Ranked Duel 1v1"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": 98.2,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-16.png",
"name": "Champion I"
},
"value": 16,
"displayValue": "16",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": 88.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"deltaDown": 13,
"deltaUp": 6,
"name": "Division III"
},
"value": 2,
"displayValue": "2",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": 57.0,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 2,
"displayValue": "2",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": 60.0,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "win"
},
"value": 1,
"displayValue": "1",
"displayType": "Number"
},
"rating": {
"rank": 101541,
"percentile": 96.1,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1031,
"displayValue": "1,031",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 11,
"season": 17
},
"metadata": {
"name": "Ranked Doubles 2v2"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": 87.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-16.png",
"name": "Champion I"
},
"value": 16,
"displayValue": "16",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": 90.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"deltaDown": 15,
"deltaUp": 3,
"name": "Division IV"
},
"value": 3,
"displayValue": "3",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": 80.0,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 40,
"displayValue": "40",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": 34.0,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "loss"
},
"value": 1,
"displayValue": "-1",
"displayType": "Number"
},
"rating": {
"rank": 311789,
"percentile": 89.0,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1177,
"displayValue": "1,177",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 13,
"season": 17
},
"metadata": {
"name": "Ranked Standard 3v3"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": 96.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-17.png",
"name": "Champion II"
},
"value": 17,
"displayValue": "17",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": 79.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"deltaDown": 7,
"deltaUp": 27,
"name": "Division III"
},
"value": 2,
"displayValue": "2",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": 97.8,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 95,
"displayValue": "95",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": 16.0,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "loss"
},
"value": 2,
"displayValue": "-2",
"displayType": "Number"
},
"rating": {
"rank": 122500,
"percentile": 95.8,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1255,
"displayValue": "1,255",
"displayType": "Number"
}
}
},
...

Get different values from repeating item JSON

I have this json derived dict:
{
"stats": [
{
"name": "Jengas",
"time": 166,
"uid": "177098244407558145",
"id": 1
},
{
"name": "- k",
"time": 20,
"uid": "199295228664872961",
"id": 2
},
{
"name": "MAD MARX",
"time": "0",
"uid": "336539711785009153",
"id": 3
},
{
"name": "loli",
"time": 20,
"uid": "366299640976375818",
"id": 4
},
{
"name": "Woona",
"time": 20,
"uid": "246996981178695686",
"id": 5
}
]
}
I want to get the "time" from everybody in the list and use it with sort.
So the result I get has this:
TOP 10:
Jengas: 166
Loli: 20
My first try is to list different values from repeating item.
Right now the code is:
with open('db.json') as json_data:
topvjson = json.load(json_data)
print(topvjson)
d = topvjson['stats'][0]['time']
print(d)
Extract the stats list, apply sort to it with the appropriate key:
from json import loads
data = loads("""{
"stats": [{
"name": "Jengas",
"time": 166,
"uid": "177098244407558145",
"id": 1
}, {
"name": "- k",
"time": 20,
"uid": "199295228664872961",
"id": 2
}, {
"name": "MAD MARX",
"time": "0",
"uid": "336539711785009153",
"id": 3
}, {
"name": "loli",
"time": 20,
"uid": "366299640976375818",
"id": 4
}, {
"name": "Woona",
"time": 20,
"uid": "246996981178695686",
"id": 5
}]
}""")
stats = data['stats']
stats.sort(key = lambda entry: int(entry['time']), reverse=True)
print("TOP 10:")
for entry in stats[:10]:
print("%s: %d" % (entry['name'], int(entry['time'])))
This prints:
TOP 10:
Jengas: 166
- k: 20
loli: 20
Woona: 20
MAD MARX: 0
Note that your time is neither an integer nor string: there are both 0 and "0" in the dataset. That's why you need the conversion int(...).
You can sort the list of dict values like:
Code:
top_three = [(x[1], -x[0]) for x in sorted(
(-int(user['time']), user['name']) for user in stats['stats'])][:3]
This works by taking the time and the name and building a tuple. The tuples can the be sorted, and then the names can be extracted (via: x[1]) after the sort.
Test Code:
stats = {
"stats": [{
"name": "Jengas",
"time": 166,
"uid": "177098244407558145",
"id": 1
}, {
"name": "- k",
"time": 20,
"uid": "199295228664872961",
"id": 2
}, {
"name": "MAD MARX",
"time": "0",
"uid": "336539711785009153",
"id": 3
}, {
"name": "loli",
"time": 20,
"uid": "366299640976375818",
"id": 4
}, {
"name": "Woona",
"time": 20,
"uid": "246996981178695686",
"id": 5
}]
}
top_three = [x[1] for x in sorted(
(-int(user['time']), user['name']) for user in stats['stats'])][:3]
print(top_three)
Results:
[('Jengas', 166), ('- k', 20), ('Woona', 20)]
Here's a way to do it using the built-in sorted() function:
data = {
"stats": [
{
"name": "Jengas",
"time": 166,
"uid": "177098244407558145",
"id": 1
},
{
etc ...
}
]
}
print('TOP 3')
sorted_by_time = sorted(data['stats'], key=lambda d: int(d['time']), reverse=True)
for i, d in enumerate(sorted_by_time, 1):
if i > 3: break
print('{name}: {time}'.format(**d))
Output:
TOP 3
Jengas: 166
- k: 20
loli: 20

Getting Sections of an API using requests

I am trying to get a section of the fortnite API that is in lifeTimeStats and the kills value. I can't figure out how to just get the kills and not the whole API.
import requests
headers = {'TRN-Api-Key': '52f2fbde-018d-47b5-9c7d-99990c0d3507'}
url = 'https://api.fortnitetracker.com/v1/profile/pc/loopisjones'
r = requests.get(url, headers = headers)
test = r.text
print test
These are the results there are too many character to enter the whole thing but I want to just get the kills from the "lifeTimeStats":
{
"accountId": "649eb4a8-584e-4896-adc9-66569ee92139",
"platformId": 3,
"platformName": "pc",
"platformNameLong": "PC",
"epicUserHandle": "loopisjones",
"stats": {
"p2": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1111,
"value": "1111",
"rank": 2507075,
"displayValue": "1,111"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 97533,
"value": "97533",
"rank": 279372,
"displayValue": "97,533"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 3,
"value": "3",
"rank": 978112,
"percentile": 48.0,
"displayValue": "3"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 56,
"value": "56",
"rank": 577434,
"percentile": 6.0,
"displayValue": "56"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 176,
"value": "176",
"rank": 318099,
"percentile": 3.2,
"displayValue": "176"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 1.04,
"value": "1.04",
"rank": 1484862,
"percentile": 42.0,
"displayValue": "1.04"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 0.5,
"value": "0.5",
"rank": 1549361,
"percentile": 61.0,
"displayValue": "0.50"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 658,
"value": "658",
"rank": 277505,
"percentile": 12.0,
"displayValue": "658"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 680,
"value": "680",
"rank": 470461,
"percentile": 18.0,
"displayValue": "680"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 1.03,
"value": "1.03",
"rank": 1484405,
"percentile": 41.0,
"displayValue": "1.03"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 323.43,
"value": "323.43",
"rank": 1535727,
"displayValue": "5m 23s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 148.23,
"value": "148.23",
"rank": 1272686,
"percentile": 53.0,
"displayValue": "148.23"
}
},
"p10": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1200,
"value": "1200",
"displayValue": "1,200"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 46764,
"value": "46764",
"rank": 1123263,
"displayValue": "46,764"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 3,
"value": "3",
"rank": 1383465,
"percentile": 48.0,
"displayValue": "3"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 20,
"value": "20",
"rank": 1438657,
"percentile": 15.0,
"displayValue": "20"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 56,
"value": "56",
"rank": 1335530,
"percentile": 14.0,
"displayValue": "56"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 0.68,
"value": "0.68",
"rank": 2133720,
"percentile": 68.0,
"displayValue": "0.68"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 0.9,
"value": "0.9",
"rank": 1752091,
"percentile": 55.0,
"displayValue": "0.90"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 347,
"value": "347",
"rank": 944115,
"percentile": 30.0,
"displayValue": "347"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 234,
"value": "234",
"rank": 1291960,
"percentile": 40.0,
"displayValue": "234"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 0.67,
"value": "0.67",
"rank": 2137868,
"percentile": 68.0,
"displayValue": "0.67"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 273.72,
"value": "273.72",
"rank": 2125448,
"displayValue": "4m 33s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 134.77,
"value": "134.77",
"rank": 2058752,
"percentile": 63.0,
"displayValue": "134.77"
}
},
"p9": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1121,
"value": "1121",
"rank": 2180620,
"displayValue": "1,121"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 98339,
"value": "98339",
"rank": 880324,
"displayValue": "98,339"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 8,
"value": "8",
"rank": 1488695,
"percentile": 31.0,
"displayValue": "8"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 36,
"value": "36",
"rank": 1240417,
"percentile": 13.0,
"displayValue": "36"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 80,
"value": "80",
"rank": 1113740,
"percentile": 11.0,
"displayValue": "80"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 0.94,
"value": "0.94",
"rank": 1775409,
"percentile": 54.0,
"displayValue": "0.94"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 1.3,
"value": "1.3",
"rank": 2230627,
"percentile": 49.0,
"displayValue": "1.30"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 603,
"value": "603",
"rank": 677528,
"percentile": 14.0,
"displayValue": "603"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 562,
"value": "562",
"rank": 873151,
"percentile": 21.0,
"displayValue": "562"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 0.93,
"value": "0.93",
"rank": 1731964,
"percentile": 53.0,
"displayValue": "0.93"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 221.59,
"value": "221.59",
"rank": 2532805,
"displayValue": "3m 41s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 163.08,
"value": "163.08",
"rank": 2029616,
"percentile": 42.0,
"displayValue": "163.08"
}
},
"curr_p2": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1111,
"value": "1111",
"displayValue": "1,111"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 22500,
"value": "22500",
"rank": 470532,
"percentile": 26.0,
"displayValue": "22,500"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1238303,
"displayValue": "0"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 16,
"value": "16",
"rank": 526184,
"percentile": 6.0,
"displayValue": "16"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 43,
"value": "43",
"rank": 384684,
"percentile": 3.9,
"displayValue": "43"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 1.03,
"value": "1.03",
"rank": 1459203,
"displayValue": "1.03"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 149,
"value": "149",
"rank": 455652,
"percentile": 19.0,
"displayValue": "149"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 153,
"value": "153",
"rank": 637148,
"percentile": 26.0,
"displayValue": "153"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 1.03,
"value": "1.03",
"rank": 1446183,
"percentile": 51.0,
"displayValue": "1.03"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 70.87,
"value": "70.87",
"rank": 962042,
"percentile": 39.0,
"displayValue": "1m 10s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 151.01,
"value": "151.01",
"rank": 1366860,
"percentile": 65.0,
"displayValue": "151.01"
}
},
"curr_p10": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1200,
"value": "1200",
"displayValue": "1,200"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 13669,
"value": "13669",
"rank": 1152025,
"percentile": 42.0,
"displayValue": "13,669"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 1,
"value": "1",
"rank": 1151758,
"displayValue": "1"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 7,
"value": "7",
"rank": 1141708,
"percentile": 15.0,
"displayValue": "7"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 22,
"value": "22",
"rank": 903205,
"percentile": 11.0,
"displayValue": "22"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 0.75,
"value": "0.75",
"rank": 1887251,
"displayValue": "0.75"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 1.0,
"value": "1",
"rank": 1443516,
"percentile": 54.0,
"displayValue": "1.00"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 96,
"value": "96",
"rank": 936423,
"percentile": 35.0,
"displayValue": "96"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 71,
"value": "71",
"rank": 1252315,
"percentile": 46.0,
"displayValue": "71"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 0.74,
"value": "0.74",
"rank": 1893112,
"percentile": 71.0,
"displayValue": "0.74"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 26.88,
"value": "26.88",
"rank": 2497616,
"percentile": 91.0,
"displayValue": "26s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 142.39,
"value": "142.39",
"rank": 1954451,
"percentile": 71.0,
"displayValue": "142.39"
}
},
"curr_p9": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1121,
"value": "1121",
"displayValue": "1,121"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 48923,
"value": "48923",
"rank": 498058,
"percentile": 8.0,
"displayValue": "48,923"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 4,
"value": "4",
"rank": 1164590,
"displayValue": "4"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 16,
"value": "16",
"rank": 861117,
"percentile": 11.0,
"displayValue": "16"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 38,
"value": "38",
"rank": 634089,
"percentile": 7.0,
"displayValue": "38"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 1.02,
"value": "1.02",
"rank": 1482777,
"displayValue": "1.02"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 1.2,
"value": "1.2",
"rank": 1953805,
"percentile": 52.0,
"displayValue": "1.20"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 322,
"value": "322",
"rank": 188452,
"percentile": 2.8,
"displayValue": "322"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 324,
"value": "324",
"rank": 455442,
"percentile": 10.0,
"displayValue": "324"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 1.01,
"value": "1.01",
"rank": 1435682,
"percentile": 52.0,
"displayValue": "1.01"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 27.02,
"value": "27.02",
"rank": 2559533,
"percentile": 91.0,
"displayValue": "27s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 151.93,
"value": "151.93",
"rank": 2317854,
"percentile": 64.0,
"displayValue": "151.93"
}
}
},
"lifeTimeStats": [
{
"key": "Top 3",
"value": "56"
},
{
"key": "Top 5s",
"value": "20"
},
{
"key": "Top 3s",
"value": "36"
},
{
"key": "Top 6s",
"value": "80"
},
{
"key": "Top 12s",
"value": "56"
},
{
"key": "Top 25s",
"value": "176"
},
{
"key": "Score",
"value": "242,636"
},
{
"key": "Matches Played",
"value": "1608"
},
{
"key": "Wins",
"value": "14"
},
{
"key": "Win%",
"value": "1%"
},
{
"key": "Kills",
"value": "1476"
},
{
"key": "K/d",
"value": "0.93"
},
{
"key": "Kills Per Min",
"value": "0.2"
},
{
"key": "Time Played",
"value": "5d 2h 37m "
},
{
"key": "Avg Survival Time",
"value": "4m 34s"
}
],
"recentMatches": [
{
"id": 71608051,
"accountId": "649eb4a8-584e-4896-adc9-66569ee92139",
"playlist": "p2",
"kills": 15,
"minutesPlayed": 0,
"top1": 0,
"top5": 0,
"top6": 0,
"top10": 2,
"top12": 0,
"top25": 4,
"matches": 16,
"top3": 0,
"dateCollected": "2018-03-21T12:26:33.713",
"score": 2018,
"platform": 3,
"trnRating": 1111.8,
"trnRatingChange": -9.6963567258982941
},
{
"id": 71608050,
"accountId": "649eb4a8-584e-4896-adc9-66569ee92139",
"playlist": "p9",
"kills": 7,
"minutesPlayed": 0,
"top1": 0,
"top5": 0,
"top6": 2,
"top10": 0,
"top12": 0,
"top25": 0,
"matches": 11,
"top3": 0,
"dateCollected": "2018-03-21T12:26:33.697",
"score": 1342,
"platform": 3,
"trnRating": 1121.4,
"trnRatingChange": -9.6722971885223643
},
...
You can use the built-in .json() function of the requests module.
headers = {'TRN-Api-Key': '52f2fbde-018d-47b5-9c7d-99990c0d3507'}
url = 'https://api.fortnitetracker.com/v1/profile/pc/loopisjones'
r = requests.get(url, headers=headers)
data = r.json()
for item in data['lifeTimeStats']:
if item['key'] == 'Kills':
print(item['value'])
Output:
1520

Categories