Consider this json file named h.json
I want to convert this into a python dataclass.
{
"acc1":{
"email":"acc1@example.com",
"password":"acc1",
"name":"ACC1",
"salary":1
},
"acc2":{
"email":"acc2@example.com",
"password":"acc2",
"name":"ACC2",
"salary":2
}
}
I could use an alternative constructor for getting each account, for example:
import json
from dataclasses import dataclass
@dataclass
class Account(object):
email:str
password:str
name:str
salary:int
@classmethod
def from_json(cls, json_key):
file = json.load(open("h.json"))
return cls(**file[json_key])
but this is limited to what arguments (email, name, etc.) were defined in the dataclass.
What if I were to modify the json to include another thing, say age?
The script would end up returning a TypeError
, specifically TypeError: __init__() got an unexpected keyword argument 'age'
.
Is there a way to dynamically adjust the class attributes based on the keys of the dict (json object), so that I don't have to add attributes each time I add a new key to the json?