Serialize and Deserialize JSON to objects in Python

Serialize & De Serialize

Hello, readers! In this article, we will be focusing on the concept of Serialization and Deserialization of JSON to objects in Python.

So, let us get started!! 馃檪

When it comes to dealing with data and APIs, we encounter either a dictionary or a JSON format of data. At times, we need functions that enable us to perform interconversion amongst them. We will be having a look at some of the methods to have the data serialized as well as de-serialized.

Also read: Python JSON module


Serialization of JSON data in Python

Serialization is the process wherein we convert the data type of the raw data into a JSON format. With that, we mean to say, that the raw data usually a dictionary will now follow the Javascript Object Notation format.

For the same, Python offers us the below functions to easily have our data formulated to JSON–

  1. json.dump() function
  2. json.dumps() function

The json.dump() function

In json.dump() function, it accepts the raw data as input, converts the data into a JSON format, and then stores it into a JSON file.

Syntax:

json.dump(data, file-object)
  • data: The actual data that needs to be converted to JSON format.
  • file-object: It is the object that would be pointing to the JSON file where the converted data will be stored. In case the file does not exist, then a new file gets created at the location pointed by the object.

Example:

import json
 
data= {
    "details": {
        "name": "YZ",
        "subject": "Engineering",
        "City": "Pune"
    }
}
 
with open( "info.json" , "w" ) as x:
    json.dump( data, x )

The json.dumps() function

Unlike dump() function, json.dumps() function does convert the raw data into JSON format but stores it as a string rather than pointing it to a file object.

Syntax:

json.dumps(data)

Example:

import json
 
data= {
    "details": {
        "name": "YZ",
        "subject": "Engineering",
        "City": "Pune"
    }
}
res = json.dumps(data)
print(res)

Output–

{"details": {"name": "YZ","subject": "Engineering","City": "Pune"}}

De-Serialization of JSON data

Having understood about deserialization, now let’s reverse the process.

That is, with deserialization, we can easily convert the JSON data into the default/native data type which is usually a dictionary.

For the same, Python offers us the below functions to implement the concept of De-serialization–

  1. json.load() function
  2. json.loads() function

The json.load() function

Here, the load() function enables us to convert the JSON data into the native dictionary format.

Syntax:

json.load(data)

Example:

In this example, we have first loaded the JSON file using the open() function. Post which, we pass the object referring the JSON file to the load() function and deserialize it into dictionary form.

import json
 
data = open('info.json',)
 
op = json.load(data)
 
print(op)
print("Datatype after de-serialization : " + str(type(op)))

Output:

{"details": {"name": "YZ","subject": "Engineering","City": "Pune"}}
Datatype after de-serialization : <class 'dict'>

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning! 馃檪