is there a way to add another value into a key instead of replacing it in json file using python? I tried using 'a' instead of 'w', but it created a different set of dictionary and I got an error message saying Json only allow one top level value
import json
web=input('web:')
username=input('us:')
pw=input('pw')
account_data= {web: {'Email': username,'Password': pw}}
try:
with open('trial.json', 'r') as file:
# save the previous data in json
data = json.load(file)
# update the json by appending the newer data into old data
data.update(account_data)
except FileNotFoundError:
with open('trial.json', 'w') as file:
# create data.json if it doesnt exist
json.dump(account_data, file, indent=4)
else:
with open('trial.json', 'w') as file:
# replace old json file with new one
json.dump(data, file, indent=4)
output:
{
"face": {
"Email": "asdqweerqr",
"Password": "dregtertsdf"
},
"fasd": {
"Email": "wqe",
"Password": "asdf"
}
}
the output always replaces the existing key,value pair. Thanks