Convert Python object to JSON | Example code

Use json.dumps() method to converts Python object into a JSON string. You need to do is to pass the dictionary representation of the object as a parameter to this method.

json.dumps(Object obj)

Python object to JSON example

Simple example code of conversion of the class object to JSON is using JSON package in Python.

You have to import the JSON package into the program file.

import json

# object (dict):
dic_obj = {
    "name": "John",
    "class": "IV",
    "age": 10


# Convert into JSON:
res = json.dumps(dic_obj)



Convert Python object to JSON

Do comment if you have any doubts and suggestions on this Python Object and JSON topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.