Serialization and (De)Serialization of Data - Python

Want to use JSON-formatted data in Python or use Python objects in JSON strings, then you are at the right place!

ยท

3 min read

Serialization and (De)Serialization of Data - Python

Introduction

In my journey of learning Python, I had no idea when I heard these terms for the first time. On doing a lot of research, I came across some articles. I will try to explain it in a simpler way.

The main purpose of serialization and deserialization is to retrieve, save and send data without disturbing its original state. These are very helpful in transmitting data and also working with API responses. Let's say we are getting an API endpoint (ex. weather API) response in JSON format and want to modify (using some programming languages like Python) and use these data in some other way then to achieve this we want some way to convert JSON data into the required re-usable format.

JSON (JavaScript Object Notation) is a human-readable format of data. It is completely language-independent and makes it an ideal data-interchange language.

Conversion table

Python data types and its respective JSON data types.

Python data typeJSON data type
list, tuplearray
int, floatnumber
dictobject
strstring
Truetrue
Falsefalse
Nonenull

Serialization

Python objects of data type dictionary, Boolean, None, etc are converted into their respective JSON data type format.

dumps() and dump() are the two methods of json module provided by python for serialization.

Dumps() :-

Serializes python objects to JSON string.

import json
dictionary = { "id": 123, "name": "xyz", "profile_id": None, "submitted": False}
json_data = json.dump(dictionary)
print(json_data)

Output: {"id": 123, "name": "xyz", "profile_id": null, "submitted": false}

You can see None is converted into null and False is converted into false.

Dump() :-

Serializes to a file.

import json
dictionary = { "id": 123, "name": "xyz", "profile_id": None, "submitted": False}
with open( "json_file.json" , "w" ) as json_file:
    json.dump( dictionary , json_file )

Output: {"id": 123, "name": "xyz", "profile_id": null, "submitted": false}

You will see a JSON file (json_file.json) has been created with the above output.

Deserialization

Deserializing JSON data back to python objects.

json module of python provides loads() and load() methods to achieve this.

Loads() :-

Deserializes JSON strings to Python objects.

import json
json_string = '{ "id": 123, "name": "xyz", "profile_id": null, "submitted": false}'
dictionary = json.loads(json_string)
print(dictionary)

Output: {'id': 123, 'name': 'xyz', 'profile_id': None, 'submitted': False}

You can see null is converted into None and false is converted into False.

Load() :-

Reads JSON data from a file and then converts it into a python object.

import json   
# Open json_data file
json_data = open('json_data.json')
dictionary = json.load(json-data)
print(dictionary)

Output: {'id': 123, 'name': 'xyz', 'profile_id': None, 'submitted': False}

ย