Save login details to json using python

Viewed 588

I was making a login form using python terminal. I took all details from external entries.json file. It worked fine! But I also wanted to make that if a user has not logged in they have to enter their name and password so I can save it in the file. But there is problem in saving. It doesn't save where I want it to. Please help me.
My code:-

import json

def write_json(new_data, filename='entries.json'):
    with open(filename,'r+') as file:
        file_data = json.load(file)
        file_data.update(new_data)
        file.seek(0)
        json.dump(file_data, file, indent = 4)
 
f = open('entries.json', 'r+')
content = json.load(f)

out_file = open('entries2.json', 'w')

print('Welcome to our company!')
print('Pease enter your username here to login.')

name = input('Enter name: ')

for c in content:

    for e in content['Entries']:

        if name == e['User name']:
            print('Please enter your password.')
            password = input('Enter password: ')
            if password == e['Password']:
                print('Welcome,', name, 'you are now logged in!')
            else:
                print('Wrong password.')
            break

    else:
        print('Your name is not in the file.')
        print('You need to register.')
        new_name = input('Please enter your name: ')
        new_password = input('Please enter your password: ')
        id = len(content['Entries'])
    
        new_full = {"User name":f"{new_name}",
                        "Password": f"{new_password}",
                        "ID": f"{id + 1}"
                        }

        write_json(new_full)
        print(new_full)
        print('Your name is added in the file.')

Json file:-

{
    "Entries": [
        {
            "User name": "Jerry",
            "Password": "56_jerry_56",
            "ID": 1
        },
        {
            "User name": "Willy_Wonka",
            "Password": "1111",
            "ID": 2
        },
        {
            "User name": "Anita",
            "Password": "438200219",
            "ID": 3
        },
        {
            "User name": "John",
            "Password": "john1",
            "ID": 4
        }
    ]
}

My problem:-
I want it to add the new_full inside the Entries but it adds it after entries in json file.
It does like this!

{
    "Entries": [
        {
            "User name": "Jerry",
            "Password": "56_jerry_56",
            "ID": 1
        },
        {
            "User name": "Willy_Wonka",
            "Password": "1111",
            "ID": 2
        },
        {
            "User name": "Anita",
            "Password": "438200219",
            "ID": 3
        },
        {
            "User name": "John",
            "Password": "john1",
            "ID": 4
        }
    ],
    "User name": "arya",
    "Password": "arya1",
    "ID": "5"
}

But I want it to be like this!

{
    "Entries": [
        {
            "User name": "Jerry",
            "Password": "56_jerry_56",
            "ID": 1
        },
        {
            "User name": "Willy_Wonka",
            "Password": "1111",
            "ID": 2
        },
        {
            "User name": "Anita",
            "Password": "438200219",
            "ID": 3
        },
        {
            "User name": "John",
            "Password": "john1",
            "ID": 4
        },
        {
            "User name": "arya",
            "Password": "arya1",
            "ID": 5
        }
    ]
}

THANK YOU!

1 Answers

Try this code in the function write_json:

def write_json(new_data, filename='entries.json'):
    with open(filename,'r+') as file:
        file_data = json.load(file)
        content['Entries'].append(new_data)
        file_data.update(content)
        file.seek(0)
        json.dump(file_data, file, indent = 4)
Related