0

I'm struggling to deserialize my json-file into an array of objects of my class LeagueAcc.

I wanna fill a list with objects of class LeagueAcc called:

accounts = []

I have loaded my JSON successfully with this:

with open('dataprint.json', 'r') as openfile:
    json_object = json.load(openfile)

Current Solution(not working)

I've added the return LeagueAcc function into my class and tried to call it .

def from_json(json_dct):
      return LeagueAcc(
          json_dct['elo'],
          json_dct['id'],
          json_dct['loginname'],
          json_dct['puuid'],
          json_dct['pw'],
          json_dct['summoner'],
          json_dct['tagline'])```

This is the call from the main code:

json_deserialized = json.loads(json_object, object_hook=LeagueAcc.LeagueAcc.from_json)
class LeagueAcc:
     def __init__(self, loginname,pw, summoner, tagline):
          self.loginname = loginname
          self.summoner = summoner
          self.pw = pw
          self.tagline = tagline
          self.elo = None
          self.id = None
          self.puuid = None

Json File

{
  "LeagueAcc": [
    {
      "elo": "MASTER 98 LP",
      "id": "321",
      "loginname": "pet",
      "puuid": "321",
      "pw": "peter",
      "summoner": "ottie",
      "tagline": "888"
    },
    {
      "elo": "MASTER 98 LP",
      "id": "123",
      "loginname": "petereerr",
      "puuid": "123",
      "pw": "peterererreer",
      "summoner": "ottie",
      "tagline": "888"
    }
  ]
}

How can I get a list accounts[] filled with two (2) objects of class LeagueAcc successfully initialized and filled with the data from the JSON?

2
  • The problem with your method is that object_hook will be called for the top-level object, not just the objects in the list.
    – Barmar
    Commented Jul 4 at 22:56
  • Your __init__ takes 4 arguments, but you pass 7 in from_json (and probably in wrong order). Did you intend to accept all 7 instead of setting three of attributes to None? If so, that'd be a perfect dataclass, and you'll be able to load with [LeagueAcc.from_json(entry) for entry in json.loads(json_object)].
    – STerliakov
    Commented Jul 5 at 2:10

1 Answer 1

0

To reduce amount of boilerplate code I'd suggest to use something like pydantic / dataclasses / etc:

from pydantic import BaseModel

body = {
    "LeagueAcc": [
        {
            "elo": "MASTER 98 LP",
            "id": "321",
            "loginname": "pet",
            "puuid": "321",
            "pw": "peter",
            "summoner": "ottie",
            "tagline": "888"
        },
        {
            "elo": "MASTER 98 LP",
            "id": "123",
            "loginname": "petereerr",
            "puuid": "123",
            "pw": "peterererreer",
            "summoner": "ottie",
            "tagline": "888"
        }
    ]
}


class LeagueAcc(BaseModel):
    elo: str
    id: str
    loginname: str
    puuid: str
    pw: str
    summoner: str
    tagline: str


league_accs = [LeagueAcc.model_validate(item) for item in body["LeagueAcc"]]

Or even more:

class LeagueAccs(BaseModel):
    league_acc: list[LeagueAcc] = Field(alias="LeagueAcc")


league_accs = LeagueAccs.model_validate(body)
1
  • I don't fully understand why this works but its working! Thank you so much.
    – Peter K
    Commented Jul 5 at 0:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.