Why am i getting this error: AttributeError: 'list' object has no attribute 'load'

Question:

When ever I try to load data from a json file I get this error:
AttributeError: ‘list’ object has no attribute ‘load’

What I expected

import json
users=[]
with open('username project.json','w') as f:
    jfile=json.dump(users,f)

def create_usernames():
    username=input('Enter in username')
    with open('username project.json','r+') as read:
        usersj=users.load(read)
    if username in users[usersj]:
            print('username rejected')
    else:
        print('username is okay')
create_usernames()

What resulted

usersj=users.load(read)
AttributeError: 'list' object has no attribute 'load'
Asked By: somehelplease

||

Answers:

import json
users=[]
with open('username project.json','w') as f:
    jfile=json.dump(users,f)

def create_usernames():
    username=input('Enter in username')
    with open('username project.json','r+') as users: #changed this to users
       usersj=users.load(read)
    if username in users[usersj]:
            print('username rejected')
    else:
        print('username is okay')
create_usernames()

are you trying to load the json users? because list doesn’t have any load method

Answered By: Sid

There’re couple of typos in your code, and I corrected accordingly

import json
users=[]
with open('username project.json','w') as f:
    jfile=json.dump(users,f)

def create_usernames():
    username=input('Enter in username')
    with open('username project.json','r+') as read:
        usersj=json.load(read) # <-- updated this
    if username in usersj: # <-- and this
            print('username rejected')
    else:
        print('username is okay')
create_usernames()

Tested both the scenarios where a username exists and does not exist

Answered By: Kulasangar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.