Making a command line login application except there seems to be an error

I am using this code:

import requests
import json
import urllib.request
import random

enteredUserName = str(input("nnEnter your username to start signing in ≥ "))

stuff = json.loads(requests.get("https://pastebin.com/raw/HJxYck9H").text)

# UserName = str(stuff['users'][enteredUserName]['username'])

if enteredUserName == "anonymous":
    print("nnYou are logging into the public account. Password for the public account is anonymous.")

enteredPassword = str(input("nnEnter your password to finish signing in ≥ "))

if enteredPassword == str(stuff['users'][enteredUserName]['password']):
    if enteredUserName == "anonymous":
        print("nnHello anonymous user. Welcome to the public account.")
    else:
        print("nnHello @" + enteredUserName + ". Welcome to your account.nnThis is your user info. Do not give it away.nUsername is " + enteredUserName + "nPassword is " + enteredPassword + "nn")
else:
    print("nnWrong password.")

The contents of the JSON on pastebin are:

{
  "users": {
    "anonymous": [
      {
        "password": "anonymous"
      }
    ],
    "James123": [
      {
        "password": "Jam3s"
      }
    ],
    "Jack123": [
      {
        "password": "[email protected]"
      }
    ]
  }
}

But when I finish entering in the username and password it shows this error:

Traceback (most recent call last):
  File "~/Documents/Test/Test.py", line 17, in <module>
    if enteredPassword == str(stuff['users'][enteredUserName]['password']):
TypeError: list indices must be integers or slices, not str

I’ve converted everything to string and even unconverted but nothing works!

Answer

This is your structure:

{
  "users": {
    "user_name": [ { "password": ... } ],
    ...
  }
}

But you access it in the wrong way, follow the steps:

  1. stuff['users'] is dict
  2. stuff['users']['user_name'] is a list

Now you can’t access a list with a string (like stuff['users']['user_name']['password']).

You should access the list for example with:

stuff['users'][enteredUserName][0]['password']

Or either with a for if you need to check more then one element

for element in stuff['users']['user_name']:
  # your check here using element['password']