Skip to content
Related Articles

Related Articles

Deserialize JSON to Object in Python
  • Last Updated : 20 Aug, 2020

Let us see how to deserialize a JSON document into a Python object. Deserialization is the process of decoding the data that is in JSON format into native data type. In Python, deserialization decodes JSON data into a dictionary(data type in python).

We will be using these methods of the json module to perform this task :

  • loads() : to deserialize a JSON document to a Python object.
  • load() : to deserialize a JSON formatted stream ( which supports reading from a file) to a Python object.

Example 1 : Using the loads() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import json
  
# creating the JSON data as a string
data = '{"Name" : "Romy", "Gender" : "Female"}'
  
print("Datatype before deserailization : "
      + str(type(data)))
   
# deserailizing the data
data = json.loads(data)
  
print("Datatype after deserailization : "
      + str(type(data)))

chevron_right


Output :

Datatype before deserailization : 
Datatype after deserailization : 
Example 2 : Using the load() function. We have to deserialize a file named file.json.



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import json
  
# opening the JSON file 
data = open('file.json',) 
  
print("Datatype before deserailization : "
      + str(type(data)))
     
# deserailizing the data
data = json.load(data) 
  
print("Datatype after deserailization : "
      + str(type(data)))

chevron_right


Output :

Datatype before deserailization : 
Datatype after deserailization : 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :