Table of contents
1.
Introduction
2.
Syntax of json.dumps() in Python
3.
Parameters of json.dump() in Python
4.
Return Values of json.dump() in Python
5.
Usage of JSON Dumps Function
5.1.
1. Converting a Dictionary to a JSON String
5.2.
Python
5.3.
2. Converting a List to a JSON String
5.4.
Python
5.5.
3. Format the JSON String with Indentation
5.6.
Python
5.7.
4. Serialising a Python Object to a JSON String
5.8.
Python
6.
Difference between JSON dump () and JSON dumps ()
7.
Frequently Asked Questions
7.1.
How to parse JSON dumps?
7.2.
What does dump() do?
7.3.
Is JSON easy to parse?
8.
Conclusion
Last Updated: Aug 22, 2025
Medium

JSON Dumps() in Python

Author Aditya Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JSON stands for Javascript Object Notation, used for computer communication and information sharing. Consider the scenario when you want to send any JSON to another computer.

JSON Dumps

To convert the JSON object into a string that can be communicated over the internet, you can use json.dumps(). It resembles packaging your recipe in an envelope to present to a friend. 

JSON (JavaScript Object Notation) is an open data interchange format. It can also be called a simple data exchange format for humans and machines to read, write, parse, and produce. It is a widely used data format for servers and online applications.

The JSON module in Python contains a method called dump that turns a Python object into a JSON string. When we want to save and transfer objects (Python objects) into a file in the form of JSON format, we make use of the Dump function. The return type of json.dumps() is a string.

Syntax of json.dumps() in Python

Following is the syntax of json.dumps().

json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
  • The object to be converted to JSON is specified by the obj parameter. The other optional parameters provide the JSON output with more customisation choices. Its data type is Python Object. 
  • skipkeys: If set to True, keys in dictionaries that are not of a primary type (str, int, float, bool, or None) will not cause a TypeError but instead will be skipped. Its data type is Boolean. 
  • ensure_ascii: If set to False, non-ASCII characters will not be escaped to Unicode code points but will be printed as-is. Its data type is Boolean. 
  • indent: The indent parameter is used to specify how the output in JSON string is formatted. This makes JSON strings more readable and easier to understand. Its data type is an integer.
  • The output dictionary keys will be arranged alphabetically if sort_keys is set to True. Its data type is Boolean. 
  • A default function will be called if an object cannot be serialized. It must either produce a TypeError or return a JSON-serializable object version. The default action is to raise a TypeError if nothing else is given.
  • Circular references in the serialized object can be verified using the check_circular argument. The JSON module will check for circular references, which are object references. It causes an infinite loop when traversing the object tree if check_circular is set to True. Its data type is Boolean. 
  • Using the separators argument, you can specify the separators to be used in the output JSON string.  By default, a comma (,) is used to separate object items and a colon (:) is used to separate keys from values in dictionaries. Its datatype is a character, mainly a comma and colon.

Parameters of json.dump() in Python

The json.dump() function in Python is used to write JSON data to a file-like object. Here are the commonly used parameters:

  • obj: This is the Python object to be serialized to JSON format.
  • fp: This is the file-like object where the JSON data will be written.
  • skipkeys: A boolean value (default is False). If set to True, it skips keys in the obj that are not serializable.
  • ensure_ascii: A boolean value (default is True). If set to True, it escapes non-ASCII characters as Unicode escape sequences. Setting it to False allows non-ASCII characters to be written as is.
  • check_circular: A boolean value (default is True). Setting to False turns off checking for circular references within the data structure.

Return Values of json.dump() in Python

The json.dump() function in Python does not return a value. It writes the serialized JSON to the file-like object specified by the fp parameter. If you want to get the serialized JSON as a string, you can use the json.dumps() function instead.
The json.dumps() function takes the same parameters as the json.dump() function returns the serialized JSON as a string instead of writing it to a file.

Usage of JSON Dumps Function

Here are some of the uses of the JSON dumps function.

1. Converting a Dictionary to a JSON String

Json Dumps can be used to convert a dictionary into a JSON String.

  • Python

Python

import json

data = {'name': 'John', 'age': 30, 'city': 'New York'}
json_string = json.dumps(data)
print(json_string)
You can also try this code with Online Python Compiler
Run Code


Output:

output

Explanation

Here, the data dictionary is passed as an argument to json.dumps(), which returns a JSON formatted string representing the dictionary.

2. Converting a List to a JSON String

Json Dumps is also used to convert a list into a JSON String.

  • Python

Python

import json

data = ['apple', 'banana', 'cherry']
json_string = json.dumps(data)
print(json_string)
You can also try this code with Online Python Compiler
Run Code


Output:

output

Explanation

Here, the list is passed as an argument to json.dumps(), which returns a JSON formatted string representing the data list.

3. Format the JSON String with Indentation

Now we will discuss an example using the indent parameter to format the JSON string with indentation.

  • Python

Python

import json

data = {'name': 'John', 'age': 30, 'city': 'New York'}
json_string = json.dumps(data, indent=4)
print(json_string)
You can also try this code with Online Python Compiler
Run Code


Output:

output

Explanation

In this code, Dictionary is passed as the first argument, and indent=4 is also given for the specific indentation and json.dumps() makes the indentation with four spaces.

4. Serialising a Python Object to a JSON String

Let us take an example of using a custom encoder to serialise a Python object to a JSON string.

  • Python

Python

import json

class Person:
   def __init__(self, name, age):
       self.name = name
       self.age = age
  
def person_encoder(obj):
   if isinstance(obj, Person):
       return {'name': obj.name, 'age': obj.age}
   else:
       raise TypeError('Object of type Person is not JSON serializable')
      
person = Person('John', 30)
json_string = json.dumps(person, default=person_encoder)
print(json_string)
You can also try this code with Online Python Compiler
Run Code


Output:

output

Explanation

In this code, a Person class is defined with two arguments, name and age. json.dumps() method is called with the person as the first argument and the person_encoder() function as the default argument. This tells the json module to use the person_encoder() function to encode any Person objects it encounters during serialization.

Difference between JSON dump () and JSON dumps ()

Differencejson.dump()json.dumps ()
Output DestinationWrites JSON data to a file-like objectReturns a JSON-encoded string
Return ValueNoneJSON-encoded string
UsageUsed for saving JSON data to a fileUsed to obtain a JSON-encoded string
Function SignatureTakes two arguments: the Python object and the file-like objectTakes one argument: the Python object to be serialized
Common Use CaseUsed when you want to store JSON data in a file or another writable objectUsed when you need a JSON string for further processing or transmission

Frequently Asked Questions

How to parse JSON dumps?

You can parse JSON dumps using json.loads() in Python, which converts a JSON string into a Python dictionary or list.

What does dump() do?

json.dump() serializes Python objects into JSON format and writes it to a file. It is used for saving data in JSON format.

Is JSON easy to parse?

Yes, JSON is easy to parse because it’s a lightweight, text-based format that’s straightforward to convert into native data structures using built-in functions like JSON.parse() in JavaScript or json.loads() in Python.

Conclusion

This article discusses the topic of JSON Dumps. We hope this blog has helped you enhance your knowledge of JSON Dumps

Recommended Readings:

Live masterclass