I'm learing on how json files work, and I was tryng to make something that I thought it was simple, but I've been having problems, I have this code:
import json
id = 696969969696
warns = 10
with open('warn.json', 'r', encoding='utf-8') as f:
guilds_dict = json.load(f)
guilds_dict[str(id)] = warns
with open('warn.json', 'w', encoding='utf-8') as f:
json.dump(guilds_dict, f, indent=4, ensure_ascii=False)
That when is executed adds id
and warns
to the JSON file looking like this:
{
"696969969696": "10"
}
So, I wanted to do that in case that id
already exists adds the numbers that warn
has, here is an example:
id = 696969969696
warns = 2
{
"696969969696": "10"
}
after executed:
{
"696969969696": "12"
}
How can I do this, and if it's possible i would like to understand it not only copy paste. Thanks!