user9156472
user9156472

Reputation:

Parsing Data from A JSON file with python and checking it

I have a JSON file that just has this:

 {
  "jeff": {

   }
 }

I would like to check if i enter the name jeff in my input it will just print 123 I have tried it this way in python:

import json
from pprint import pprint

with open('users.json') as f:
    data = json.load(f)

print("Hi, would you like to  sign up or login?")
print("Type Login for login and Signup to signup")

option= input()

if option == "Login":
    unameEntry = input("Please Enter Your Username")
    if unameEntry == data[unameEntry] :
        print("123")

but it does not find it i have looked around on google and have strougled to find an answer

Upvotes: 3

Views: 645

Answers (2)

semore_1267
semore_1267

Reputation: 1437

Made a few tweaks to what you have, and added some comments for detail. Here's what the Python would look like:

# Python3
import json


# Load json file one-liner
file_json = json.load(open("test.json", "r"))

# Since options can only be signup/login, santize them
option = input("Type Login for login and Signup to signup: ").strip().lower()
if option == "login":
    # Get name entry and strip it of whitespace
    name_entry = input("Please Enter Your Username: ").strip()
    # Use .get() on file_json dict() object, with a default option
    # of None. However, with this you actually have to add data to 
    # your JSON and not just a empty field
    if file_json.get(name_entry, None):
        print("123")
    else:
        print("Name: {}, not found".format(name_entry))
else:
    print("unrecognized option: {}".format(option))

And here's what the test JSON should look like:

{
    "jeff": {
        "foo": "bar"
    }
}

Notice how I've added a default value to "jeff". Else, doing a lookup will always return nothing when you've got { } as "jeff"'s value

Upvotes: 0

Luca Cappelletti
Luca Cappelletti

Reputation: 2545

What is wrong

When you access the value in your dictionary with:

data[unameEntry]

the dictionary returns you the value at data[unameEntry]. Suppose that, as you have set in your example, the user types in "jeff": following your example the dictionary will return the corresponding value, in your example an empty dictionary.

Therefore your if is actually doing:

"jeff" == {}

that is clearly False.

How to fix it

Most probably you want to check if the user is a key in the dictionary. You can do so by using the in operator:

unameEntry in data

Edited code

Since here we do not have access to your users.json file, let's include the dictionary in the code. Also, to detect if errors are found within the input handling, I'll through in a couple of asserts and expand the conditions tree:

data = {
    "jeff": {}
}

print("Hi, would you like to  sign up or login?")
print("Type Login for login and Signup to signup")

option= input()

assert option == "Login"

if option == "Login":
    unameEntry = input("Please Enter Your Username")
    assert unameEntry == "jeff"
    if unameEntry in data:
        print("Welcome %s!" % unameEntry)
    else:
        print("Username %s does not exist" % unameEntry)

Upvotes: 1

Related Questions