How to deserialize JSON to custom class objects in Python

Deserializing JSON to custom class objects just requires some cautionsPhoto by Carolina Garcia Tavizon on Unsplash· 1. Custom class definition· 2. json.loads() with object_hook· 3. Nested objects· 4. jsonpickle· ConclusionThis is a follow-up article on…

Deserializing JSON to custom class objects just requires some cautions

Photo by Carolina Garcia Tavizon on Unsplash

· 1. Custom class definition
· 2. json.loads() with object_hook
· 3. Nested objects
· 4. jsonpickle
· Conclusion

This is a follow-up article on How to Serialize a Class Object to JSON in Python to close the loop on the issues of serialization and deserialization in Python. Serialization is turning an object into strings or files and to get the original objects back from the saved strings is deserialization. If you understand how serialization is done, you can pretty much reverse the course for deserialization. However, as is usually the case, the devil is in the details. Let’s start with the definition of a custom class first.

1. Custom class definition

To match the illustration with the previous article, let’s use the same simple Label class first

class Label:
def __init__(self, label, x, y, width, height):
self.label = label
self.x = x
self.y = y
self.width = width
self.height = height

The serialized string using json.dumps() is:

{"label": "person", "x": 10, "y": 10, "width": 4, "height": 10}

2. json.loads() with object_hook

The default method of deserialization is json.loads() which takes a string as an input and outputs a JSON dictionary object. To convert the dictionary object to a custom class object, you need to write a deserialize method. The easiest way is to add a static method to the class itself. The entire class with the serialize (to_json) and deserialize (from_json) methods should look like this:

Armed with the deserialization method, we can call json.loads method and specify Label class’s new deserialization method as the object_hook:

The loads() method, instead of returning a JSON dictionary object, yields a proper Label object as expected.

3. Nested objects

So far so good. Now let’s see what you need to do for more complex cases where you have nested objects. The ImageLabelCollection class contains a list of Label objects as a part of the class variable ‘bboxes’. The definition of the class is copied below with the serialization method to_json().

A sample of a resulting json string is:

To deserialize the string to a class object, you need to write a custom method to construct the object. You can add a static method to ImageLabelCollection inside of which you construct Label objects from the loaded JSON dictionary and then assign them as a list to the class variable bbox.

The process is a little tedious but there is a better way. You can write from_json() static method where you handle constructing the class object incrementally.

When json.loads() is called with the above from_json() method as the object hook, it will call the method iteratively as it builds the object bottom up.

The catch is that the method is called multiple times and each time, a different part of the dictionary object is called so you have to add the logic of handling how to turn dictionary objects into proper class objects depending on the types. In the above case, I used the presence of certain keys as a cue to decide which constructor should be called. It is still a little tedious, but better than the original solution of writing the entire code yourself.

4. jsonpickle

The final method of deserialization is using an external library called jsonpickle. You can easily install it by:

pip install jsonpickle

The usage is quite simple. You call jsonpickle.encode() to serialize and decode() to deserialize.

The result of serialization is slightly different from our original JSON string:

{"py/object": "__main__.ImageLabelCollection", "version": 1, "type": "bounding-box-labels", "bboxes": {"20210715_111300 16.jpg": [{"py/object": "__main__.Label", "label": "StabilityOff", "x": 1, "y": 1025, "width": 553, "height": 29}, {"py/object": "__main__.Label", "label": "StabilityOn", "x": 1, "y": 964, "width": 563, "height": 30}]}}

It added some additional annotation information to deserialize later.

Conclusion

Given the simplicity of jsonpickle, it seems that for general purposes, this should be the easiest method to serialize. However, the limitation is that due to jsonpickle’s additional attributes in JSON strings, you need to use jsonpickle’s decode() method to bring the objects back. This should not be a problem in general, but if you have some custom objects that you need to handle on your own, you can use the suggested custom deserialization method. The entire code sample is shared here. Thanks for reading.


How to deserialize JSON to custom class objects in Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Changsin Lee | Sciencx (2024-03-29T13:31:55+00:00) » How to deserialize JSON to custom class objects in Python. Retrieved from https://www.scien.cx/2021/12/29/how-to-deserialize-json-to-custom-class-objects-in-python/.
MLA
" » How to deserialize JSON to custom class objects in Python." Changsin Lee | Sciencx - Wednesday December 29, 2021, https://www.scien.cx/2021/12/29/how-to-deserialize-json-to-custom-class-objects-in-python/
HARVARD
Changsin Lee | Sciencx Wednesday December 29, 2021 » How to deserialize JSON to custom class objects in Python., viewed 2024-03-29T13:31:55+00:00,<https://www.scien.cx/2021/12/29/how-to-deserialize-json-to-custom-class-objects-in-python/>
VANCOUVER
Changsin Lee | Sciencx - » How to deserialize JSON to custom class objects in Python. [Internet]. [Accessed 2024-03-29T13:31:55+00:00]. Available from: https://www.scien.cx/2021/12/29/how-to-deserialize-json-to-custom-class-objects-in-python/
CHICAGO
" » How to deserialize JSON to custom class objects in Python." Changsin Lee | Sciencx - Accessed 2024-03-29T13:31:55+00:00. https://www.scien.cx/2021/12/29/how-to-deserialize-json-to-custom-class-objects-in-python/
IEEE
" » How to deserialize JSON to custom class objects in Python." Changsin Lee | Sciencx [Online]. Available: https://www.scien.cx/2021/12/29/how-to-deserialize-json-to-custom-class-objects-in-python/. [Accessed: 2024-03-29T13:31:55+00:00]
rf:citation
» How to deserialize JSON to custom class objects in Python | Changsin Lee | Sciencx | https://www.scien.cx/2021/12/29/how-to-deserialize-json-to-custom-class-objects-in-python/ | 2024-03-29T13:31:55+00:00
https://github.com/addpipe/simple-recorderjs-demo