Now the JSON module provides a lot of functionality and we’re going to discuss only 2 methods out of them. They are dumps and loads. The process of converting a python object to a json one is called JSON serialization or encoding and the reverse process i.e. converting json object to Python one is called deserialization or decoding
import json
py_object = {"c": 0, "b": 0, "a": 0}
json_string = json.dumps(py_object)
print(json_string)
print(type(json_string))
py_obj = json.loads(json_string)
print()
print(py_obj)
print(type(py_obj))
|
Output:
{"c": 0, "b": 0, "a": 0}
<class 'str'>
{'c': 0, 'b': 0, 'a': 0}
<class 'dict'>
Although what we saw above is a very simple example. But have you wondered what happens in the case of custom objects? In that case he above code will not work and we will get an error something like – TypeError: Object of type SampleClass is not JSON serializable. So what to do? Don’t worry we will get to get in the below section.
Encoding and Decoding Custom Objects
In such cases, we need to put more efforts to make them serialize. Let’s see how we can do that. Suppose we have a user-defined class Student and we want to make it JSON serializable. The simplest way of doing that is to define a method in our class that will provide the JSON version of our class’ instance.
Example:
import json
class Student:
def __init__(self, name, roll_no, address):
self.name = name
self.roll_no = roll_no
self.address = address
def to_json(self):
return json.dumps(self, indent = 4, default=lambda o: o.__dict__)
class Address:
def __init__(self, city, street, pin):
self.city = city
self.street = street
self.pin = pin
address = Address("Bulandshahr", "Adarsh Nagar", "203001")
student = Student("Raju", 53, address)
student_json = student.to_json()
print(student_json)
print(type(student_json))
student = json.loads(student_json)
print(student)
print(type(student))
|
Output:
{
“name”: “Raju”,
“roll_no”: 53,
“address”: {
“city”: “Bulandshahr”,
“street”: “Adarsh Nagar”,
“pin”: “203001”
}
}
<class ‘str’>
{‘name’: ‘Raju’, ‘roll_no’: 53, ‘address’: {‘city’: ‘Bulandshahr’, ‘street’: ‘Adarsh Nagar’, ‘pin’: ‘203001’}}
<class ‘dict’>
Another way of achieving this is to create a new class that will extend the JSONEncoder and then using that class as an argument to the dumps method.
Example:
import json
from json import JSONEncoder
class Student:
def __init__(self, name, roll_no, address):
self.name = name
self.roll_no = roll_no
self.address = address
class Address:
def __init__(self, city, street, pin):
self.city = city
self.street = street
self.pin = pin
class EncodeStudent(JSONEncoder):
def default(self, o):
return o.__dict__
address = Address("Bulandshahr", "Adarsh Nagar", "203001")
student = Student("Raju", 53, address)
student_JSON = json.dumps(student, indent = 4,
cls = EncodeStudent)
print(student_JSON)
print(type(student_JSON))
student = json.loads(student_JSON)
print()
print(student)
print(type(student))
|
Output:
{
“name”: “Raju”,
“roll_no”: 53,
“address”: {
“city”: “Bulandshahr”,
“street”: “Adarsh Nagar”,
“pin”: “203001”
}
}
<class ‘str’>
{‘name’: ‘Raju’, ‘roll_no’: 53, ‘address’: {‘city’: ‘Bulandshahr’, ‘street’: ‘Adarsh Nagar’, ‘pin’: ‘203001’}}
<class ‘dict’>
For Custom Decoding, if we want to convert the JSON into some other Python object (i.e. not the default dictionary) there is a very simple way of doing that which is using the object_hook parameter of the loads method. All we need to do is to define a method that will define how do we want to process the data and then send that method as the object_hook argument to the loads method, see in given code. Also, the return type of load will no longer be the Python dictionary. Whatever is the return type of the method we’ll pass in as object_hook, it will also become the return type of loads method. This means that in following example, complex number will be the return type.
import json
def as_complex(dct):
if '__complex__' in dct:
return complex(dct['real'], dct['imag'])
return dct
res = json.loads('{"__complex__": true, "real": 1, "imag": 2}',
object_hook = as_complex)
print(res)
print(type(res))
|
Output:
(1+2j)
<class 'complex'>