JSON dumps Python | Function

Python JSON dumps() function converts a Python object into a json string. This function is used when the objects are required to be in string format and is used for parsing, printing, etc.

json.dumps(obj, indent=4)

Example JSON dumps Python

Simple example code passing the Python dictionary to json.dumps() function will return a string.

The example converts the input dictionary into a string.

import json

# Dictionary
Dictionary = {1: 'A', 2: 'B', 3: 'C'}

res = json.dumps(Dictionary)
print(res)
print(type(res))

Output:

JSON dumps Python

You can convert Python objects of the following types, into JSON strings:

  • dict
  • list
  • tuple
  • string
  • int
  • float
  • True
  • False
  • None

Do comment if you have any doubts and suggestions on this Python dumps() function.

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.