Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding nested python object in JSON

I want to encode objects in JSON. But, I can not figure out how to make the output without the string escaping.

import json  class Abc:     def __init__(self):         self.name="abc name"     def toJSON(self):         return json.dumps(self.__dict__, cls=ComplexEncoder)  class Doc:     def __init__(self):         self.abc=Abc()     def toJSON(self):         return json.dumps(self.__dict__, cls=ComplexEncoder)  class ComplexEncoder(json.JSONEncoder):     def default(self, obj):         if isinstance(obj, Abc) or isinstance(obj, Doc):             return obj.toJSON()         else:             return json.JSONEncoder.default(self, obj)  doc=Doc() print doc.toJSON() 

The result is (the dumps returns a string representation, that's why the " are escaped)

{"abc": "{\"name\": \"abc name\"}"} 

I want something a little bit different. The expected result is

{"abc": {"name": "abc name"}"} 

But I don't see how to... Any hint ?

thanks in advance.

like image 936
Fred Laurent Avatar asked Mar 01 '11 20:03

Fred Laurent


People also ask

How do you serialize and deserialize a JSON object in Python?

For serializing and deserializing of JSON objects Python “__dict__” can be used. There is the __dict__ on any Python object, which is a dictionary used to store an object's (writable) attributes. We can use that for working with JSON, and that works well.

What's the difference between JSON dump and JSON dumps?

json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.


1 Answers

my previous sample, with another nested object and your advices :

import json  class Identity:     def __init__(self):         self.name="abc name"         self.first="abc first"         self.addr=Addr()     def reprJSON(self):         return dict(name=self.name, firstname=self.first, address=self.addr)   class Addr:     def __init__(self):         self.street="sesame street"         self.zip="13000"     def reprJSON(self):         return dict(street=self.street, zip=self.zip)   class Doc:     def __init__(self):         self.identity=Identity()         self.data="all data"     def reprJSON(self):         return dict(id=self.identity, data=self.data)   class ComplexEncoder(json.JSONEncoder):     def default(self, obj):         if hasattr(obj,'reprJSON'):             return obj.reprJSON()         else:             return json.JSONEncoder.default(self, obj)  doc=Doc() print "Str representation" print doc.reprJSON() print "Full JSON" print json.dumps(doc.reprJSON(), cls=ComplexEncoder) print "Partial JSON" print json.dumps(doc.identity.addr.reprJSON(), cls=ComplexEncoder) 

produces the expected result :

Str representation {'data': 'all data', 'id': <__main__.Identity instance at 0x1005317e8>} Full JSON {"data": "all data", "id": {"name": "abc name", "firstname": "abc first", "address": {"street": "sesame street", "zip": "13000"}}} Partial JSON {"street": "sesame street", "zip": "13000"} 

Thanks.

like image 73
Fred Laurent Avatar answered Oct 08 '22 12:10

Fred Laurent