i want to put the return of my API listener to a json file. Unfortunately when i run the code below only the empty dictionary does get printed to the jsonfile. I don't see why, does anyone know why it is like that?
from chessdotcom import get_player_game_archives
import pprint
import requests
import pymongo
import json
uName = input()
printer = pprint.PrettyPrinter()
global game
game = {}
def get_most_recent_game(username):
data = get_player_game_archives(username).json
url = data['archives'][-1]
games = requests.get(url).json()
game = games['games'][-1]
printer.pprint(game)
return(game)
get_most_recent_game(uName)
with open('Games.json', 'w') as json_file:
json.dump(game, json_file)
game = get_most_recent_game(uName).get_most_recent_gameto work with the global game object you would putglobal gameinside the definition ofget_most_recent_game. As it is, it's better to avoid global variables. I would just removegameas a global variable.