2

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!

  • Append the new information before writing - `content['Entries'].append(new_full)`. Then save the whole object with json.dump. – wwii May 30 '21 at 13:55
  • Your file contains a json object with one item in it, (Entities as a key and list of entities as a value), and what happens when you use update() is that you are updating the parent json object, not the entities list, so when you load the file do as @wwii suggested, or instead save a list directly into the json file, you do not need redefine another json inside your json file, just take the list of entries and dump it into json, I hope this makes sense. – notify_my_threads May 30 '21 at 13:56
  • [Why is `with open()` better for opening files in Python?](https://stackoverflow.com/questions/19711344/why-is-with-open-better-for-opening-files-in-python). [https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) – wwii May 30 '21 at 14:02
  • Yesssss it worked thank you guys so much! – Dr. Strange Codes May 30 '21 at 14:04

1 Answers1

1

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)