JSON as we know stands for JavaScript Object Notation. It is a lightweight data-interchange format and has become the most popular medium of exchanging data over the web. The reason behind its popularity is that it is both human-readable and easy for machines to parse and generate. Also, it’s the most widely used format for the REST APIs.
Note: For more information, refer to Read, Write and Parse JSON using Python
Getting Started
Python provides a built-in json
library to deal with JSON objects. All you need is to import the JSON module using the following line in your Python program and start using its functionality.
import json
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
For encoding, we use json.dumps()
and for decoding, we’ll use json.loads()
. So it is obvious that the dumps method will convert a python object to a serialized JSON string and the loads method will parse the Python object from a serialized JSON string.
Note:
- It is worth mentioning here that the JSON object which is created during serialization is just a Python string, that’s why you’ll find the terms “JSON object” and “JSON string” used interchangeably in this article
- Also it is important to note that a JSON object corresponds to a Dictionary in Python. So when you use loads method, a Python dictionary is returned by default (unless you change this behaviour as discussed in the custom decoding section of this article)
Example:
import json # A basic python dictionary py_object = { "c" : 0 , "b" : 0 , "a" : 0 } # Encoding json_string = json.dumps(py_object) print (json_string) print ( type (json_string)) # Decoding JSON 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 ): ''' convert the instance of this class to json ''' 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) # Encoding student_json = student.to_json() print (student_json) print ( type (student_json)) # Decoding 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) # Encoding custom object to json # using cls(class) argument of # dumps method student_JSON = json.dumps(student, indent = 4 , cls = EncodeStudent) print (student_JSON) print ( type (student_JSON)) # Decoding 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'>
<!–
–>
Please Login to comment…