In this post, we looks into converting a plain, simple Python object into JSON. JSON serialization in Java is also provided as an example. In the following post, we will look into a more advanced method of conversion with attributes pretty-printed in order, like in the Java example.
JSON serialization in Java
In Java, it is pretty straight-forward to convert Java objects (POJO) to JSON using Jackson library. The following code will convert an example POJO to JSON:
1 2 3 4 5 6 7  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13  |  | 
The JSON output is shown below.
Note that the keys (e.g., “type”, “host”) appear in the same order as defined in the Config class.
This will become important later when we try to convert Python objects to JSON.
1 2 3 4 5 6 7  |  | 
JSON serialization in Python
In Python, we have json module to convert a serializable object to JSON format.
The first attempt at JSON serialization in Python may look like this, with a slightly complex Python object is intentionally used as an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56  |  | 
This first attempt with json.dump(config, config_file) will fail with the following error:
1
 |  | 
As the message indicates, Config object is not JSON serializable.
json.dump function expects a serializable object such as one of Python standard object types (see Python to JSON mapping table below) or their subclasses.
| Python | JSON | 
|---|---|
| dict | object | 
| list, tuple | array | 
| str, unicode | string | 
| int, long, float | number | 
| True | true | 
| False | false | 
| None | null | 
The solution for that problem is to specify the default parameter with a function that returns object’s __dict__ attribute.
__dict__ is the internal attribute dictionary that contains all attributes associated with an object.
Object attribute references are translated to lookups in this dictionary, e.g., o.x is translated to o.__dict__["x"].
1 2  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  |  | 
Here, we use vars built-in function to retrieve the object’s __dict__ attribute.
Note that simply using json.dump(vars(config), config_file) will NOT work if any attribute of the object is another complex object (e.g., source and target attributes in this example).
For more complex objects such as those include sets, we may have to define our own Encoder that extends json.JSONEncoder and provide it to json.dump function.
The next post will discuss how to print keys in order of which they are defined, like in the Java example.