Menu Close

How to Convert Python Class Object to JSON

how to convert python class object to json

In this article, we are going to learn all about how to convert Python class object to JSON string data. Here, we will use Python built-in module named JSON to convert any python class object to JSON data.

Python JSON module provide a dumps() function that is very useful to convert Python class object to JSON data.
Click here to learn all about the Python JSON module.

In this guide, we will also see how to store various class objects into an array or list after converted into JSON. Every Python object has an attribute __dict__ that gives all the properties of the object.

How to Convert Python Class Object to JSON

There are two steps to convert python class to JSON data which are as follows.

  • Firstly, Get all the properties of object using __dict__ attribute.
  • Now, pass those vaule (Which comes from __dict__ ) into dumps() function.

If you did not understand the above process, don’t worry at the end of this article, you will be completely able to convert any Python class object into JSON string data.

Let see

Example 1:


from json import dumps


class Students:
    def __init__(self, name, age, course, roll_no):
        self.student_name = name
        self.student_age = age
        self.student_course = course
        self.student_roll_no = roll_no


# create first object
first = Students("Vishvajit Rao", 21, "BCA", "E100")

# get all the attributes of first object
props = first.__dict__  # This will give:- {'student_name': 'Vishvajit Rao', 'student_age': 21, 'student_course':
# 'BCA', 'student_roll_no': 'E100'}


# # convert into json
result = dumps(props)
print(result)

Output

{"student_name": "Vishvajit Rao", "student_age": 21, "student_course": "BCA", "student_roll_no": "E100"}

Example 2:


from json import dumps


class Students:
    def __init__(self, name, age, course, roll_no):
        self.student_name = name
        self.student_age = age
        self.student_course = course
        self.student_roll_no = roll_no


# create multiple objects

first = Students("Vishvajit Rao", 21, "BCA", "E100")
second = Students("John", 24, "BTech", "J100")
third = Students("Shayna", 22, "MCA", "S100")
fourth = Students("Harry", 30, "BTech + MTech", "H100")

print(dumps(first.__dict__))
print(dumps(second.__dict__))
print(dumps(third.__dict__))
print(dumps(fourth.__dict__))

Output


{"student_name": "Vishvajit Rao", "student_age": 21, "student_course": "BCA", "student_roll_no": "E100"}
{"student_name": "John", "student_age": 24, "student_course": "BTech", "student_roll_no": "J100"}
{"student_name": "Shayna", "student_age": 22, "student_course": "MCA", "student_roll_no": "S100"}
{"student_name": "Harry", "student_age": 30, "student_course": "BTech + MTech", "student_roll_no": "H100"}

As you can see in the above code, we have printed all the JSON separately but suppose you want to store all the JSON into the list.

Python Tutorials

Example 3:


from json import dumps


class Students:
    def __init__(self, name, age, course, roll_no):
        self.student_name = name
        self.student_age = age
        self.student_course = course
        self.student_roll_no = roll_no


# create multiple objects

first = Students("Vishvajit Rao", 21, "BCA", "E100")
second = Students("John", 24, "BTech", "J100")
third = Students("Shayna", 22, "MCA", "S100")
fourth = Students("Harry", 30, "BTech + MTech", "H100")

data = []

for obj in [first, second, third, fourth]:
    data.append(obj.__dict__)

# convert data to JSON

result = dumps(data)
print(result)

Output


[{"student_name": "Vishvajit Rao", "student_age": 21, "student_course": "BCA", "student_roll_no": "E100"}, {"student_name": "John", "student_age": 24, "student_course": "BTech", "student_roll_no": "J100"}, {"student_name": "Shayna", "student_age": 22, "student_course": "MCA", "student_roll_no": "S100"}, {"student_name": "Harry", "student_age": 30, "student_course": "BTech + MTech", "student_roll_no": "H100"}]

Conclusion

So, In this article, we have seen all about how to convert Python class object to JSON string data with the help of the Python JSON module.
dumps() function of Python JSON module is a better function to convert Python object to JSON string data.

I hope after reading this article, you wouldn’t have any confusion regarding how to convert Python class object to JSON data.
If you like our tutorial, please share and keep visit for further Python interesting tutorials.

Ref:- Click Here

Thanks for reading…

How To Sort Python Dictionary by Key or Value
How to Convert a Tuple to a DataFrame in Python

Related Posts