I am stumped in trying to output this JSON file to a dataframe. I can see the JSON content printed out on the screen, but when I try to load it to a dataframe the result is empty. Any advice gladly appreciated. The output I am looking for is show on the picture:
import json
from urllib.request import urlopen
import pandas as pd
with urlopen('https://statdata.pgatour.com/r/021/2020/player_stats.json') as response:
source = response.read()
data = json.loads(source)
tid = data['tournament']['tournamentNumber']
for item in data['tournament']['players']:
try:
pid = item['pid']
stats = item['stats']
for stat in stats:
statId = stat['statId']
name = stat['name']
tValue = stat['tValue']
print(tid, pid, statId, name, tValue)
except Exception as e:
print(e)
print(item)
break
df = pd.DataFrame (data, columns = ['tid', 'pid', 'statId', 'name', 'tValue'])
print(df)