I want to append multiple objects into a single array in a JSON file. Each object is created after executing the code and then saved into an array in a JSON file.
I have this code:
import json
users = [
{
"username": "",
"phone": ""
}
]
username = input('Username: ')
phone = input('Phone: ')
for user in users:
user['username'] = username
user['phone'] = phone
with open('users.json', 'a') as file:
json.dump(users, file, indent=4)
After executing this code once, I get this:
[
{
"username": "Mark",
"phone": "333-4743"
}
]
After executing twice I get this:
[
{
"username": "Mark",
"phone": "333-4743"
}
][
{
"username": "Jane",
"phone": "555-6723"
}
]
But I want this result:
[
{
"username": "Mark",
"phone": "333-4743"
},
{
"username": "Jane",
"phone": "555-6723"
}
]
How can I achieve this result?