Python dict to JSON | Example code

Use JSON dumps() method to convert dict to JSON string in Python. It’s a built-in function that converts the dictionary to a string object, not the json(dictionary) object!

Python dict to JSON example

In simple example code we have defined one dictionary and then convert that dictionary to JSON using json.dumps() method.

import json

dictionary = {
    "id": "100",
    "name": "John",
    "department": "HR"
}

# Serializing json  
res = json.dumps(dictionary, indent=4)
print(res)

print(type(res))

Output:

Python dict to JSON

Do comment if you have any doubts and suggestions on this Python dict 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.