So, the immediate problem is that you're passing the json module a JSON value, which will get encoded as just another string in the JSON value.
The broader problem is that you're greatly overcomplicating this.
Drawing on JSON datetime between Python and JavaScript, I'd go with something closer to this:
import jsonclass Abc: def __init__(self): self.name="abc name" def jsonable(self): return self.nameclass Doc: def __init__(self): self.abc=Abc() def jsonable(self): return self.__dict__def ComplexHandler(Obj): if hasattr(Obj, 'jsonable'): return Obj.jsonable() else: raise TypeError('Object of type %s with value of %s is not JSON serializable' % (type(Obj), repr(Obj)))doc=Doc()print json.dumps(doc, default=ComplexHandler)
which gets you:
~$ python nestjson.py {"abc": "abc name"}~$
This can be made cleaner/saner/safer (in particular, just grabbing __dict__
isn't generally a recommended thing to do outside debugging/troubleshooting), but it should get the point across. All you need, fundamentally, is a way to get a json-compatible object (whether that's a simple string or number, or a list or dict) out of each "node" in the tree. That object should not be an already-JSON-serialized object, which is what you were doing.