I am trying to populate a JSON file from the user input. The users.json file is initially empty, and I was able to register the first user ("Doe_Joh"). The problem was when I ran the program and registered for the second use. The data inside got replaced by the data. What I expected was to have the data saved incrementally. How can I achieve this?
Here is my code.
import json
class User:
def register():
first = input("Name: ")
last = input("Last: ")
username = input("Username: ")
email = input("Email: ")
user_data = { username: [ {
"fname": first,
"lname": last,
"username": username,
"email": email
}
]
}
with open("users.json", "w") as outfile:
json.dump(user_data, outfile, indent=4)
user1 = User
user1.register()
open("users.json", "a"). But the whole data won't be a json instead, it would be a json for each user one after the other. If you want a single json, you will have to read the whole file and write it again after adding the new user to the data.