Home > Mobile >  Python read JSON file dictionary
Python read JSON file dictionary

Time:09-23

Edit: Thnx for reply's everyone. I guess I was programming blind!

I was wondering if anyone could help me with this code. I'm trying to read in a JSON file and check if the E-mail and password match the data requested.I've tried various things for a few hours and every time I get new errors, that's why I decided to ask the question online otherwise I'll be tinkering for hours more.

JSON FILE users.json: [{"email": "Mark", "password": "Hi123", "role": "Owner"},{"email": "Elsje", "Password": "Hi123", "Role": "Family member"},{"email": "Fred", "Password": "Hi123", "Role": "Owner"}]

import json
import time


count = 0  # Count the number of failed login attempts starting with 0.
current_time = time.localtime()  # Add the current time.
clock = time.strftime("%I:%M:%S %p", current_time)


def open_file(filename): # Function to open files and read them.
    with open(filename) as json_file:
        test = json.load(json_file)
        return test
    

read_file = open_file("users.json")
print(read_file)
print(type(read_file))

while True:
    email = input("\nEnter your username: ")
    passwords = input("Enter your password: ")
    count  = 1  # Count the number of failed login attempts.
    login = False
    print("")

    **for item in read_file:
        if item[read_file]["email"] == email and item[read_file]["password"] == passwords:**
            print("Welcome", read_file[read_file]["email"], read_file[read_file]["role"], "you successfully logged in at", clock, "\n")
            count = 0
            login = True

    if login: break
    if not login:
        print("Incorrect E-mail or password!\n")
    if count > 5:  # I
        print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes!")
        time.sleep(900)
        break

I'm getting the following error: line 28, in if item[read_file]["email"] == email and item[read_file]["Password"] == passwords: TypeError: unhashable type: 'list'

CodePudding user response:

Here is the working code edited by the comment suggested by @Samwise

import json
import time


count = 0  # Count the number of failed login attempts starting with 0.
current_time = time.localtime()  # Add the current time.
clock = time.strftime("%I:%M:%S %p", current_time)


def open_file(filename):  
    with open(filename) as f:
        return json.load(f)


read_file = open_file("2.json")
print(read_file)
print(type(read_file))

while True:
    email = input("\nEnter your username: ")
    passwords = input("Enter your password: ")
    count  = 1  # Count the number of failed login attempts.
    login = False
    print("")

    for item in read_file:
        if item["email"] == email and item["password"] == passwords:

            print("Welcome", item["email"], item["role"], "you successfully logged in at", clock, "\n")
            count = 0
            login = True

    if login:
        break
    print("Incorrect E-mail or password!\n")
    if count > 5:  # I
        print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes!")
        time.sleep(900)
        break

CodePudding user response:

Just a little less confusion.

import json
import time

count = 0  # Count the number of failed login attempts starting with 0.
current_time = time.localtime()  # Add the current time.
clock = time.strftime("%I:%M:%S %p", time.localtime())

login = False

while not login:
    email = input("\nEnter your username: ")
    passwords = input("Enter your password: ")
    count  = 1  # Count the number of failed login attempts.

    with open('users.json') as f:
        test = json.load(f)
        for item in test:
            if item["email"] == email and item["password"] == passwords:
                print("Welcome", item["email"], item["role"], "you successfully logged in at", clock, "\n")
                count = 0
                login = True
                break

    if not login:
        print("Incorrect E-mail or password!\n")
        if count > 2:  # I
            print("On", clock, "You have logged incorrectly 5 times. You are blocked for 15 minutes!")
            time.sleep(5)
            break
  • Related