For a more "IDE-friendly" option that provides type hinting and auto completion, I'd suggest looking into the Dataclass Wizard library for this. Just a disclaimer, that it's still in beta because I'm testing some things out, but it seems like it should work well enough for this use case.
Step 1: Generate a model for the data above
You can use an included CLI utility to do this as it's easier, but there's an easy way to do it programatically as well:
from dataclass_wizard.wizard_cli import PyCodeGenerator
string = """
{
"myapps": {
"app_1": {
"username": "admin",
"pwd": "S3cret",
"ports": [
8080,
443
],
"users": {
"user1": "john",
"user2": "ruth"
}
},
"app_2": {
"username": "user1",
"pwd": "P@ssword"
}
}
}
"""
print(PyCodeGenerator(file_contents=string).py_code)
Step 2: Load data into the model
from dataclasses import dataclass
from typing import List
from dataclass_wizard import JSONWizard
@dataclass
class Data(JSONWizard):
"""
Data dataclass
"""
myapps: 'Myapps'
@dataclass
class Myapps:
"""
Myapps dataclass
"""
app_1: 'App1'
app_2: 'App2'
@dataclass
class App1:
"""
App1 dataclass
"""
username: str
pwd: str
ports: List[int]
users: 'Users'
@dataclass
class Users:
"""
Users dataclass
"""
user1: str
user2: str
@dataclass
class App2:
"""
App2 dataclass
"""
username: str
pwd: str
data = Data.from_json(string)
repr(data)
# Data(myapps=Myapps(app_1=App1(username='admin', pwd='S3cret', ports=[8080, 443], users=Users(user1='john', user2='ruth')), app_2=App2(username='user1', pwd='P@ssword')))
Now you can use the dot . access, as intended. Type hinting for attributes should also work with your IDE (at least in Pycharm)
myapps = data.myapps
print("App_2 username = ", myapps.app_2.username) # prints App_2 username = user1
print("App_2 pwd = ", myapps.app_2.pwd) # prints App_2 pwd = P@ssword
print("App_1 ports = ", myapps.app_1.ports) # prints App_1 ports = [8080, 443]
myapps.app_2.username = "MyNewAdminAccount"
print("App_2 username = ", myapps.app_2.username) # prints App_2 username = MyNewAdminAccount