I have a simple json file containing user levels and experience, in this format:
{
"users" : {
"1" : [0, 1],
"2" : [10, 2],
}
}
The users object uses the user id as keys for the [xp, level] array values. I need to be able to write a new user to the file, such that later I can call it as data["users"][id].
Here's my code so far, but it doesn't actually write to the file.
import json
def create_user(id):
with open("test.json") as file:
data = json.load(file)
temp = data["users"] # get the users object.
temp[str(id)] = [0, 0]
# [0, 0] would be the default, until the user
# gains levels and xp.
print('user added to file.')
What can I do to make it add the user to the users object and save to the file? Thanks.