Skip to content

Python dict to JSON | Example code

  • by

Use the 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 or suggestions on this Python dict JSON topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Your email address will not be published. Required fields are marked *