Not finding object in dict when its in dict

Hello, I have 2 functions that save dictionaries into JSON files and load the JSON file and save it into a dictionary. Everything works fine until I try to get the value of something, i.e. my discord id which has stored a token. There is the code:
JSON file:
{‘1234567890’: “some token”}

Discord Interaction where I call the get_auth function:

async def balance(interaction: discord.Interaction):
    if (token := await get_auth(interaction.user)) is None:
        print(token)
        print(await get_auth(interaction.user)) 
        await interaction.response.send_message("You must login before using any command")
    else:
        await interaction.response.send_message(f"Your actual balance is: {Currency.balance(token)}<:rbx:1085598507764822016>")

load_auth function:

def load_auth():
    with open("tokens.json","r") as json_file:
        jsn = json.load(json_file)
        dic = dict()
        for token in jsn:
            dic[token] = jsn[
                token
            ]
        json_file.close() 
        return dic

get_auth function:

async def get_auth(user: discord.User):
    secondtry = load_auth() 
    return auth.get(user.id) or secondtry.get(user.id) or None

If your JSON has a dictionary as its top level then you only need this:

def load_auth():
    with open("tokens.json", "r") as json_file:
        tokens = json.load(json_file)
        return tokens

If you are not sure that in tokens then using print (or pprint) to show you like this:

I change jsn to tokens to reflect what I think you are reading from the file.
As its, I asume, already a dict then just return it.

The with open() closes the file for you, no need to explicity call close
as that is the whole point of the with.

Finally please don’t post pictures of text. just cut-n-paste the text and put inside the </> preformatted text as you did for the code.

1 Like

Thanks for the info about the with open()

But it still dont work, it still returns None

Are you sure that user.id has the value you expect?

And type too - if your stored dictionary has string keys but you’re looking up an int, it won’t be found.

1 Like

Yes, it looks for the user id, which is always stored with the token in the dictionary

That made it work, thanks!