Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Convert class object to JSON in Python
Next article icon
JSON as we know stands forJavaScript Object Notation. It is a lightweight data-interchange format and has become the most popular medium of exchanging data over the web. The reason behind its popularity is that it is both human-readable and easy for machines to parse and generate. Also, it's the most widely used format for the REST APIs.Note: For more information, refer to Read,Write and Parse JSON using Python

Getting Started

Python provides a built-injson library to deal with JSON objects. All you need is to import the JSON module using the following line in your Python program and start using its functionality.
import json
Now the JSON module provides a lot of functionality and we're going to discuss only 2 methods out of them. They aredumps andloads. The process of converting a python object to a json one is calledJSON serialization orencoding and the reverse process i.e. converting json object to Python one is calleddeserialization ordecodingFor encoding, we usejson.dumps() and for decoding, we'll usejson.loads(). So it is obvious that the dumps method will convert a python object to a serialized JSON string and the loads method will parse the Python object from a serialized JSON string.Note:
  • It is worth mentioning here that the JSON object which is created during serialization is just a Python string, that's why you'll find the terms "JSON object" and "JSON string" used interchangeably in this article
  • Also it is important to note that a JSON object corresponds to a Dictionary in Python. So when you use loads method, a Python dictionary is returned by default (unless you change this behaviour as discussed in the custom decoding section of this article)
Example:Python3 1==
importjson# A basic python dictionarypy_object={"c":0,"b":0,"a":0}# Encodingjson_string=json.dumps(py_object)print(json_string)print(type(json_string))# Decoding JSONpy_obj=json.loads(json_string)print()print(py_obj)print(type(py_obj))
Output:
{"c": 0, "b": 0, "a": 0}<class 'str'>{'c': 0, 'b': 0, 'a': 0}<class 'dict'>
Although what we saw above is a very simple example. But have you wondered what happens in the case of custom objects? In that case he above code will not work and we will get an error something like -TypeError: Object of type SampleClass is not JSON serializable. So what to do? Don't worry we will get to get in the below section.

Encoding and Decoding Custom Objects

In such cases, we need to put more efforts to make them serialize. Let's see how we can do that. Suppose we have a user-defined classStudent and we want to make it JSON serializable. The simplest way of doing that is to define a method in our class that will provide the JSON version of our class' instance.Example:Python3 1==
importjsonclassStudent:def__init__(self,name,roll_no,address):self.name=nameself.roll_no=roll_noself.address=addressdefto_json(self):'''        convert the instance of this class to json        '''returnjson.dumps(self,indent=4,default=lambdao:o.__dict__)classAddress:def__init__(self,city,street,pin):self.city=cityself.street=streetself.pin=pinaddress=Address("Bulandshahr","Adarsh Nagar","203001")student=Student("Raju",53,address)# Encodingstudent_json=student.to_json()print(student_json)print(type(student_json))# Decodingstudent=json.loads(student_json)print(student)print(type(student))
Output:
{ "name": "Raju", "roll_no": 53, "address": { "city": "Bulandshahr", "street": "Adarsh Nagar", "pin": "203001" }}<class 'str'>{'name': 'Raju', 'roll_no': 53, 'address': {'city': 'Bulandshahr', 'street': 'Adarsh Nagar', 'pin': '203001'}}<class 'dict'>
Another way of achieving this is to create a new class that will extend theJSONEncoder and then using that class as an argument to thedumps method.Example:Python3 1==
importjsonfromjsonimportJSONEncoderclassStudent:def__init__(self,name,roll_no,address):self.name=nameself.roll_no=roll_noself.address=addressclassAddress:def__init__(self,city,street,pin):self.city=cityself.street=streetself.pin=pinclassEncodeStudent(JSONEncoder):defdefault(self,o):returno.__dict__address=Address("Bulandshahr","Adarsh Nagar","203001")student=Student("Raju",53,address)# Encoding custom object to json# using cls(class) argument of# dumps methodstudent_JSON=json.dumps(student,indent=4,cls=EncodeStudent)print(student_JSON)print(type(student_JSON))# Decodingstudent=json.loads(student_JSON)print()print(student)print(type(student))
Output:
{ "name": "Raju", "roll_no": 53, "address": { "city": "Bulandshahr", "street": "Adarsh Nagar", "pin": "203001" }}<class 'str'>{'name': 'Raju', 'roll_no': 53, 'address': {'city': 'Bulandshahr', 'street': 'Adarsh Nagar', 'pin': '203001'}}<class 'dict'>
ForCustom Decoding, if we want to convert the JSON into some other Python object (i.e. not the default dictionary) there is a very simple way of doing that which is using theobject_hook parameter of theloads method. All we need to do is to define a method that will define how do we want to process the data and then send that method as the object_hook argument to the loads method, see in given code. Also, the return type of load will no longer be the Python dictionary. Whatever is the return type of the method we'll pass in as object_hook, it will also become the return type of loads method. This means that in following example, complex number will be the return type.Python3 1==
importjsondefas_complex(dct):if'__complex__'indct:returncomplex(dct['real'],dct['imag'])returndctres=json.loads('{"__complex__": true, "real": 1, "imag": 2}',object_hook=as_complex)print(res)print(type(res))
Output:
(1+2j)<class 'complex'>

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp