Serialize and Deserialize complex JSON in Python

JSON stands for JavaScript Object Notation. It is a format that encodes the data in string format. JSON is language independent and because of that, it is used for storing or transferring data in files. The conversion of data from JSON object string is known as Serialization and its opposite string JSON object is known as Deserialization. JSON Object is defined using curly braces{} and consists of a key-value pair. It is important to note that the JSON object key is a string and its value can be any primitive(e.g. int, string, null) or complex data types(e.g. array).

Example of JSON Object:

{
 "id":101,
 "company" : "GeeksForGeeks"
}

Complex JSON objects are those objects that contain a nested object inside the other. Example of Complex JSON Object.

{
 "id":101,
 "company" : "GeeksForGeeks",
 "Topics" : { "Data Structure",
              "Algorithm",
              "Gate Topics" }
}

Serialization & Deserialization

Python and the JSON module is working extremely well with dictionaries. For serializing and deserializing of JSON objects Python “__dict__” can be used. There is the __dict__ on any Python object, which is a dictionary used to store an object’s (writable) attributes. We can use that for working with JSON, and that works well.
Code: 



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import json
  
  
class GFG_User(object):
    def __init__(self, first_name: str, last_name: str):
        self.first_name = first_name
        self.last_name = last_name
          
user = GFG_User(first_name="Jake", last_name="Doyle")
json_data = json.dumps(user.__dict__)
print(json_data)
print(GFG_User(**json.loads(json_data)))

chevron_right


Output: 

{"first_name": "Jake", "last_name": "Doyle"} 
__main__.GFG_User object at 0x105ca7278

Note: The double asterisks ** in the GFG_User(**json.load(json_data) line may look confusing. But all it does is expanding the dictionary.

Complex Objects

Now things get tricky while dealing with complex JSON objects as our trick “__dict__” doesn’t work anymore.
Code: 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from typing import List
import json
  
  
class Student(object):
    def __init__(self, first_name: str, last_name: str):
        self.first_name = first_name
        self.last_name = last_name
  
  
class Team(object):
    def __init__(self, students: List[Student]):
        self.students = students
  
  
student1 = Student(first_name="Geeky", last_name="Guy")
student2 = Student(first_name="GFG", last_name="Rocks")
team = Team(students=[student1, student2])
json_data = json.dumps(team.__dict__, indent=4)
print(json_data)

chevron_right


Output: 

TypeError: Object of type Student is not JSON serializable

But if you look at the documentation of dump function you will see there is a default setting that we can use. Simply by replacing this line:

json_data = json.dumps(team.__dict__, indent=4)

By this line:

json_data = json.dumps(team.__dict__, lambda o: o.__dict__, indent=4)

And everything works now as before. Now, let’s look at Deserializing:
Code: 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from typing import List
import json
  
  
class Student(object):
    def __init__(self, first_name: str, last_name: str):
        self.first_name = first_name
        self.last_name = last_name
  
  
class Team(object):
    def __init__(self, students: List[Student]):
        self.students = students
  
  
student1 = Student(first_name="Geeky", last_name="Guy")
student2 = Student(first_name="GFG", last_name="Rocks")
team = Team(students=[student1, student2])
  
# Serialization
json_data = json.dumps(team, default=lambda o: o.__dict__, indent=4)
print(json_data)
  
# Deserialization
decoded_team = Team(**json.loads(json_data))
print(decoded_team)

chevron_right


Output: 

{
    "students": [
        {
            "first_name": "Geeky",
            "last_name": "Guy"
        },
        {
            "first_name": "GFG",
            "last_name": "Rocks"
        }
    ]
}
__main__.Team object at 0x105cd41d0



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.