I've been experimenting with python for a while and just ran into an issue I can't find an answer to. Just started building a small 'banking' console app and it's "database" is in JSON file. Now I added a command called 'transfer' which should transfer 'funds' from one user_id to another. JSON file looks like this:
{
"users": [
{
"user_id": "u111111111",
"pin": "2222",
"balance": "50800"
},
{
"user_id": "u123456789",
"pin": "1234",
"balance": "0"
}
]
}
Last thing I tried was something like this and got completely stuck.
user_database = 'accounts.json'
ujf = json.loads(open(user_database, 'r+').read())
for element in ujf['users']:
if (element['user_id'] == str(user_id_one)):
element['balance'] = str(user_one_new_balance)
else:
pass
How do I update the 'balance' value for user_id u111111111 and for user_id u123456789 and save the file, without creating a new file?