import json
class A:
a = 1
class B:
b = 2
class Test:
a = A()
b = B()
def to_dict(obj):
d = {}
for name in dir(obj):
value = getattr(obj, name)
if not name.startswidth("__") and not callable(value):
d[name] = value
return d
test = Test()
json.loads(json.dumps(test, default=to_dict))