Home > Mobile >  how can i check that a json file already has something inside?
how can i check that a json file already has something inside?

Time:05-13

I have this json file where it contains a name and a code:

{
    "CDIA": [
        {
            "name": "Pablo",
            "cdia": "XB&DE@E TY"
        },
        {
            "name": "Elena",
            "cdia": "KB?D @EHTY"
        },
        {
            "name": "Pedro",
            "cdia": "AB?DE@E TY"
        },
        {
            "name": "Miguel",
            "cdia": "AB?DE@E TY"
        }
    ]
}

So, i need to make a function that checks the user input if its already on the json file or not, so i tried this:


name = str(input('[?] Enter your name: '))

def Check(x):
    cdia = open('CDIA.json', 'r')
    data = json.load(cdia)
    for i in data['CDIA']:
        if x == i['name']: return True
        else: return False

if val.Check(name) == False:
    print('[!] The name is already registered.')
else:
    print("[!] The name isnt registered yet.")

no matter what name i enter, it keeps saying its already registered.

CodePudding user response:

Combining all of my suggestions:

cdia = open('CDIA.json', 'r')
data = json.load(cdia)

def Check(x):
    for i in data['CDIA']:
        if x == i['name']:
            return True
    return False

name = input('[?] Enter your name: ')
if Check(name):
    print('[!] The name is already registered.')
else:
    print("[!] The name isnt registered yet.")
  • Related