Serialize and Deserialize complex JSON in Python (original) (raw)

JSON stands for **JavaScript Object Notation. It is a lightweight, language-independent format for storing and transferring data often used in web APIs, configuration files and databases.

In Python, **JSON is widely used to store data in files or exchange data between systems.

**Example:

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

Complex JSON Objects

A complex JSON object is a JSON structure that contains nested objects, arrays or a mix of both inside another object. Let’s explore some examples:

**1. Nested JSON Objects: An object inside another object.

{
"student": {
"name": "John",
"age": 20
}
}

**2. JSON Objects with Arrays: An object that contains a list.

{
"fruits": ["apple", "banana", "mango"]
}

**3. Array of JSON Objects: A list where each item is an object.

[
{ "name": "James" },
{ "name": "Jennie" }
]

**4. Mixed Nested Objects and Arrays: Objects and arrays combined.

{
"class": [
{ "name": "Sam", "marks": [85, 90, 88] },
{ "name": "Lily", "marks": [92, 87, 91] }
]
}

Serialization and Deserialization

Let’s understand this with an examples.

1. Working with Simple JSON Objects

When data stored in a Python object only contains basic types (strings, numbers, booleans, etc.), it can be easily serialized and deserialized using Python’s built-in json module and object’s __dict__ attribute.

**Example: In this example,a simple object containing only first and last names is serialized and then deserialized.

Python `

import json class GFG_User: 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")

Serialize → JSON string

json_data = json.dumps(user.dict) print(json_data)

Deserialize → Python object

data_dict = json.loads(json_data) new_user = GFG_User(**data_dict) print(new_user)

`

Output

{"first_name": "Jake", "last_name": "Doyle"} <__main__.GFG_User object at 0x7fed8f4de850>

**Explanation:

2. Handling Complex JSON Objects

When dealing with nested data where an object contains other objects the __dict__ trick no longer works directly. This is because json.dumps() doesn’t know how to serialize custom objects inside other objects.

Let’s explore this problem and see how to fix it.

**Example: This example creates a Team object that contains multiple Student objects. We try to serialize it using json.dumps(team.__dict__) without any custom handling.

Python `

from typing import List import json

class Student: def init(self, first_name: str, last_name: str): self.first_name = first_name self.last_name = last_name

class Team: def init(self, students: List[Student]): self.students = students

student1 = Student("Geeky", "Guy") student2 = Student("GFG", "Rocks") team = Team([student1, student2])

Try serialization

json_data = json.dumps(team.dict, indent=4) print(json_data)

`

**Output

TypeError: Object of type Student is not JSON serializable

**json.dumps() doesn’t know how to convert a Student object to JSON automatically. We can fix this by passing a default parameter that tells JSON how to handle unknown objects.

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

**Example: Here, we use default parameter to handle nested objects during serialization, then deserialize the JSON back to a Python object.

Python `

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)

`