Encoding and Decoding Custom Objects in Python-JSON
Last Updated :
10 Jul, 2020
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:
Python3 1==
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:
Python3 1==
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:
Python3 1==
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.
Python3 1==
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'>
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read