0
votes

I want to create a record, and I partially succeeded. But here's the problem, I can't record 2, I get the following error. What am I doing wrong?

Error :

  File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\bilgi\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Example Code :

def loadUsers(self):
        # Dosya var?
        if os.path.exists('AccInformation.json'): # True ise......
            with open('AccInformation.json', 'r', encoding='utf-8') as file:
                users = json.load(file)
                
                for user in users:
                    user = json.load(user)
                    newUser = Account(user_id = user['user_id'], firstName = user['first_name'], lastName = user['last_name'], 
                                      email = user['email'], username = user['username'], 
                                      password = user['password'], accountKEY = user['AccountKEY'])
                    
                    self.users.append(newUser)
                print(self.users)
        else:
            print("""'AccInformation' adlı Dosya bulunamadı.""")
2
what's the content of AccInformation.json? - Avión
Are you sure that AccInformation.json contains valid JSON? Also, user = json.load(user) won't work because user will be a string rather than a file descriptor - user2668284
AccInformation.json content this ; ["{\"user_id\": 518990642826, \"firstName\": \"Asdf\", \"lastName\": \"CCCC\", \"email\": \"[email protected]\", \"username\": \"Einsatzgruppens\", \"password\": \"123456\", \"accountKEY\": \"31AI-TR9F-6GMP-S7DE-KJOC-V0Z4\"}"] - Einsatzgrup

2 Answers

0
votes

So without knowing the contents of AccInformation.json it's difficult to properly answer this question, however I imagine it is a JSON with a list of dict items that represent separate users.

Based on that, the below code should work...

import os
import json


def loadUsers(self):
    # Dosya var?
    if os.path.exists("AccInformation.json"):  # True ise......
        with open("AccInformation.json", encoding="utf-8") as infile:
            users = json.load(infile)
        # return indent here as we've already loaded the file

        for user in users:
            newUser = Account(
                user_id=user["user_id"],
                firstName=user["first_name"],
                lastName=user["last_name"],
                email=user["email"],
                username=user["username"],
                password=user["password"],
                accountKEY=user["AccountKEY"],
            )

            self.users.append(newUser)
        print(self.users)
    else:
        print("""'AccInformation' adlı Dosya bulunamadı.""")
0
votes

I just solved the problem. That's exactly what the problem was : " user = json.load(user)"

Ben bunu şöyle düzelttim : "user = json.loads(user)".

He's working without problems right now. I hope he doesn't cause me any trouble in the future:)

Thank you all individually for your time.