- Python JSON – Custom Class Deserialization
- 1.1. Add Deserializer Method to the Class
- 1.2. Use ‘object_hook’ attribute on json.loads()
- 1.2. Use ‘cls’ attribute on json.dumps()
- Python Serialize and Deserialize JSON to Objects
- In Python, How do you serialize JSON data?
- De-serialization of JSON data to Native Python Object
- Serialize and Deserialize JSON to objects in Python
- Serialization of JSON data in Python
- The json.dump() function
- The json.dumps() function
- De-Serialization of JSON data
- The json.load() function
- Conclusion
Python JSON – Custom Class Deserialization
Learn to deserialize JSON string and convert deserialized JSON data to a custom class (e.g. User ) by extending JSONDecoder or object_hook method.
In Python custom deserialization example, we have following User class. Here, birthdate of type datetime and we can define any formatter string for it.
import datetime class User: def __init__(self, id, name, birthdate): self.id = id self.name = name if isinstance(birthdate, str): self.birthdate = datetime.datetime.strptime(birthdate, "%d %b %y") else: self.birthdate = birthdate
1. Python Custom Deserializer Method using object_hook
1.1. Add Deserializer Method to the Class
By default, json.load() or json.loads() methods read the JSON and return as python dictionary object. In the deserializer method, we have to convert the dict to our custom class.
The given to_object() method takes a dictionary method argument and creates a new User instance by calling its constructor.
Notice how we are using the ‘__class__’ and ‘__module__’ attributes to validate that we are decoding the JSON to correct class in the correct module. The class and module information, in this case, is part of JSON data itself.
We are free to customize the deserialization logic as we need in the application.
class User: def __init__(self, id, name, birthdate): self.id = id self.name = name if isinstance(birthdate, str): self.birthdate = datetime.datetime.strptime(birthdate, "%d %b %y") else: self.birthdate = birthdate def to_object(d): if d['__class__'] == 'User' and d['__module__'] == 'user': inst = User(d['id'], d['name'], d['birthdate']) else: inst = d return inst
1.2. Use ‘object_hook’ attribute on json.loads()
Next, to use this deserializer method, use the object_hook attribute in json.loads() method. It will be automatically called by python while converting JSON to the complex object.
import json from user import User # Python dict user_json = """< "name": "Lokesh", "id": 39, "birthdate": "06 Jan 91", "__class__": "User", "__module__": "user" >""" # Use object_hook to execute custom deserialization code user_object = json.loads(user_json, object_hook=User.to_object) # Verify if we read the valid JSON and created the User object as desired print(user_object) print(json.dumps(user_object, default=User.to_dict))
2. Python Custom Deserialization using JSONDecoder
Another way, to add custom deserialization logic, is to extend the JSONDecoder class.
The following ComplexDecoder class declares the dict_to_object() method which provides the logic to convert the JSON to the complex class.
The dict_to_object() method checks all dictionary objects read by json.loads() method and checks the ‘__class__’ and ‘__module__’ properties in dictionary.
If it finds the class and module information, dict_to_object() method imports and loads the class. It then creates a new instance of the class by calling its constructor and passing dictionary key-value pairs as constructor arguments.
import json class ComplexDecoder(json.JSONDecoder): def __init__(self): json.JSONDecoder.__init__( self, object_hook=self.dict_to_object, ) def dict_to_object(self, d): if '__class__' in d: class_name = d.pop('__class__') module_name = d.pop('__module__') module = __import__(module_name) class_ = getattr(module, class_name) args = < key: value for key, value in d.items() >inst = class_(**args) else: inst = d return inst
1.2. Use ‘cls’ attribute on json.dumps()
To use custom JSONDecoder , use the cls attribute in json.loads() method. It will be automatically called by python while converting the JSON to the complex object.
import json from complex_decoder import ComplexDecoder from user import User # Python dict user_json = """< "name": "Lokesh", "id": 39, "birthdate": "06 Jan 91", "__class__": "User", "__module__": "user" >""" # Use ComplexDecoder to execute custom deserialization code user_object = json.loads(user_json, cls=ComplexDecoder) print(user_object) print(json.dumps(user_object, default=User.to_dict))
Python Serialize and Deserialize JSON to Objects
JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. The text in JSON is represented by a quoted-string, which contains the value in the key-value mapping within flower braces < >.
In Python, How do you serialize JSON data?
Serialization is the process of converting the raw data’s data type to JSON representation. That is to say, the raw data, which is typically a dictionary, will now match the Javascript Object Notation format.
Python provides us with the following functions to simply format our data to JSON:
1)json.dump() function:
The native data of the primary data type is accepted as input by the json.dump() method. It then converts the data to JSON format and stores it in a JSON file.
data: It is the actual data that needs to be converted to JSON format is referred to as data.
fileobject: This is the object that will point to the JSON file containing the transformed data. If the file does not already exist, a new file is created at the location specified by the object.
- Import json module using the import keyword.
- Give some random data in JSON format and store it in a variable.
- Open some random file in write-mode using the open() function by passing the filename, mode as arguments to it.
- Dump the given data into the above-opened file using the dump() function. It then converts the data to JSON format.
- The Exit of the Program.
Below is the implementation:
# Import json module using the import keyword import json # Give some random data in Json format and store it in a variable gvn_data= < "demo_data": < "Article By": "Btechgeeks", "topic": "JSON", "language": "Python" >> # Open some random file in write-mode using the open() function by passing # the filename, mode as arguments to it. with open( "demo.json" , "w" ) as d: # Dump the given data into the above opened file using the dump() function json.dump( gvn_data, d )
2)json.dumps() function
In contrast to the dump() function, the json.dumps() function converts raw data to JSON format but stores it as a string rather than pointing to a file object.
- Import json module using the import keyword.
- Give some random data in JSON format and store it in a variable.
- Pass the given data as an argument to the dumps() function to convert raw data to JSON format (It stores it as a string rather than pointing to a file object).
- Store it in a variable.
- Print the above result
Below is the implementation:
# Import json module using the import keyword import json # Give some random data in Json format and store it in a variable gvn_data= < "demo_data": < "Article By": "Btechgeeks", "topic": "JSON", "language": "Python" >> # Pass the given data as an argument to the dumps() function to convert raw data to # JSON format.(It stores it as a string rather than pointing to a file object) # Store it in a variable rslt_data = json.dumps(gvn_data) # print the above result print(rslt_data)
De-serialization of JSON data to Native Python Object
That is, we can simply convert JSON data into the default/native data type, which is usually a dictionary, using deserialization.
Python provides the following functions to implement the concept of de-serialization:
json.load() Function:
We can convert JSON string data into a native dictionary object in Python using the json.load() function.
Below is the implementation:
# Import json module using the import keyword import json # Open the JSON file using the open() function by passing the filename/filepath # as an argument to it and store it in a variable file_data = open('demo.json') # Pass the above JSON file data as an argument to the load() function to deserialize it # Store it in another variable rslt = json.load(file_data) # Print the above result print(rslt) # Print the datatype of the above result using the type() function. print("After de-serialization, the datatype of the above result is:\n", type(rslt))
> After de-serialization, the datatype of the above result is:
Here, we first used the open() function to load the JSON file. Following that, we give the object corresponding to the JSON file to the load() function, where it is deserialized into dictionary form.
Serialize and Deserialize JSON to objects in Python
Hello, readers! In this article, we will be focusing on the concept of Serialization and Deserialization of JSON to objects in Python.
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.
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–
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.
- 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.
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.
import json data= < "details": < "name": "YZ", "subject": "Engineering", "City": "Pune" >> res = json.dumps(data) print(res)
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–
The json.load() function
Here, the load() function enables us to convert the JSON data into the native dictionary format.
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)))
> Datatype after de-serialization :
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.