The Wayback Machine - https://web.archive.org/web/20240903201211/https://www.geeksforgeeks.org/python-difference-between-json-dump-and-json-dumps/
Open In App

Python – Difference between json.dump() and json.dumps()

Last Updated : 02 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. 

Note: For more information, refer to Working With JSON Data in Python

json.dumps()

json.dumps() method can convert a Python object into a JSON string.

Syntax: json.dumps(dict, indent) 

Parameters:

  • dictionary – name of dictionary which should be converted to JSON object.
  • indent – defines the number of units for indentation

Example: 

Python3




# Python program to convert
# Python to JSON
    
    
import json
    
# Data to be written
dictionary ={
  "id": "04",
  "name": "sunil",
  "department": "HR"
}
    
# Serializing json 
json_object = json.dumps(dictionary, indent = 4)
print(json_object)


Output:

{
    "department": "HR",
    "id": "04",
    "name": "sunil"
}

Python objects and their equivalent conversion to JSON:

Python JSON Equivalent
dict object
list, tuple array
str string
int, float number
True true
False false
None null

json.dump()

json.dump() method can be used for writing to JSON file.

Syntax: json.dump(dict, file_pointer) 

Parameters:

  • dictionary – name of dictionary which should be converted to JSON object.
  • file pointer – pointer of the file opened in write or append mode.

Example: 

Python3




# Python program to write JSON
# to a file
  
  
import json
  
# Data to be written
dictionary ={
    "name" : "sathiyajith",
    "rollno" : 56,
    "cgpa" : 8.6,
    "phonenumber" : "9976770500"
}
  
with open("sample.json", "w") as outfile:
    json.dump(dictionary, outfile)


Output: python-json-write-to-file

Let us see the differences in a tabular form -:

  json.dump()  json.dumps()
1. json.dump() method used to write Python serialized object as JSON formatted data into a file. json.dumps() method is used to encodes any Python object into JSON formatted String.
2.

Its syntax is -:

json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

Its syntax is -:

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)
 

3. It is used to perform compact encoding to save file space It takes 7 parameters.
4. It is used to skip nonbasic types while JSON encoding It can be used with Lists.


Similar Reads

json.dumps() in Python
JSON is an acronym that stands for JavaScript Object Notation. Despite its name, JSON is a language agnostic format that is most commonly used to transmit data between systems, and on occasion, store data. Programs written in Python, as well as many other programming languages, can ingest JSON formatted data, and can serialize data in memory into t
6 min read
Use jsonify() instead of json.dumps() in Flask
Here, we will understand the jsonify() function in the Flask web framework for Python that converts the output of a function to a JSON response object. It is similar to the json.dumps() function in the Python standard library, which converts a Python object to a JSON-formatted string. What is jsonify() The jsonify() function is useful in Flask apps
4 min read
json.dump() in Python
The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json . To use this feature, we import the json package in Python script. The text in JSON is done through quoted
6 min read
PyYAML Dump Format - Python
PyYAML is a popular Python library used for parsing and writing YAML (YAML Ain't Markup Language) files. YAML is known for its simplicity and readability, making it a common choice for configuration files and data exchange. The dump function in PyYAML is used to convert Python objects into YAML format, allowing for easy serialization of data. What
4 min read
PyQt5 QCalendarWidget - Dump Calendar Info
In this article we will see how we can dump object information of the QCalendarWidget, dumping calendar information means to dump the information about signal connections, etc. for the calendar to the debug the output. In order to do this we will use dumpObjectInfo method with the QCalendarWidget object.Syntax : calendar.dumpObjectInfo()Argument :
1 min read
PyQt5 QCalendarWidget - Dump Calendar Tree
In this article we will see how we can dump object tree of the QCalendarWidget, dumping calendar tree means to dump a tree of children to the debug output. Object tree is a tree data structure holding child objects of calendar at the nodes. In order to do this we will use dumpObjectTree method with the QCalendarWidget object.Syntax : calendar.dumpO
1 min read
Python - Difference Between json.load() and json.loads()
JSON (JavaScript Object Notation) is a script (executable) file which is made of text in a programming language, is used to store and transfer the data. It is a language-independent format and is very easy to understand since it is self-describing in nature. Python has a built-in package called json. In this article, we are going to see Json.load a
3 min read
Difference between Json and Dictionary in Python
In Python, dictionaries and JSON (JavaScript Object Notation) both facilitate structured data representation, yet they diverge in usage and syntax. In this article, we will discuss the Definition of JSON and Dictionary and the difference between JSON and Dictionary in Python. JSON vs DictionaryDictionaries, native to Python, are employed for in-mem
4 min read
How to communicate JSON data between Python and Node.js ?
The following article covers how to communicate JSON data between Python and Node.js. Suppose we are working with the Node.js application, and we want to make use of a specific library that is only available in python or vice versa. We should be able to share the results from one language to another and to achieve it, we will use the JSON as it is
7 min read
Communication Between two Programs using JSON
JSON stands for JavaScript Object Notation. To communicate between computer and human we use a programming language. To communicate between two programs, there must be a common data format and here we use JSON format. Let us say you have an application with Frontend written in Python and Backend written in Java. Now you need to communicate between
3 min read
Reading and Writing JSON to a File in Python
The full form of JSON is Javascript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-
3 min read
Read, Write and Parse JSON using Python
JSON is a lightweight data format for data interchange that can be easily read and written by humans, and easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called JSON. Example of JSON String s = '{"id":01, "name": "Emily", "language": ["C++", "Python"]}
4 min read
Saving Text, JSON, and CSV to a File in Python
Python allows users to handle files (read, write, save and delete files and many more). Because of Python, it is very easy for us to save multiple file formats. Python has in-built functions to save multiple file formats. Opening a text file in Python Opening a file refers to getting the file ready either for reading or for writing. This can be don
5 min read
Encoding and Decoding Custom Objects in Python-JSON
JSON as we know stands for JavaScript Object Notation. It is a lightweight data-interchange format and has become the most popular medium of exchanging data over the web. The reason behind its popularity is that it is both human-readable and easy for machines to parse and generate. Also, it's the most widely used format for the REST APIs. Note: For
5 min read
Serialize and Deserialize complex JSON in Python
JSON stands for JavaScript Object Notation. It is a format that encodes the data in string format. JSON is language-independent and because of that, it is used for storing or transferring data in files. Serialization of JSON object: It means converting a Python object (typically a dictionary) into a JSON formatting string. Deserialization of JSON o
3 min read
Python Encode Unicode and non-ASCII characters into JSON
This article will provide a comprehensive guide on how to work with Unicode and non-ASCII characters in Python when generating and parsing JSON data. We will look at the different ways to handle Unicode and non-ASCII characters in JSON. By the end of this article, you should have a good understanding of how to work with Unicode and non-ASCII charac
5 min read
Creating a JSON Response Using Django and Python
In Django, we can give responses to the front end in several ways. The simplest way to render a template or send a JSON response. JSON stands for JavaScript Object Notation and is widely used for data transfer between front-end and back-end and is supported by different languages and frameworks. In Django, we can easily give a JSON response to the
4 min read
Fetch JSON URL Data and Store in Excel using Python
In this article, we will learn how to fetch the JSON data from a URL using Python, parse it, and store it in an Excel file. We will use the Requests library to fetch the JSON data and Pandas to handle the data manipulation and export it to Excel. Fetch JSON data from URL and store it in an Excel file in PythonThe process of fetching JSON data from
3 min read
JSON Formatting in Python
JSON (JavaScript Object Notation) is a popular data format that is used for exchanging data between applications. It is a lightweight format that is easy for humans to read and write, and easy for machines to parse and generate. Python Format JSON Javascript Object Notation abbreviated as JSON is a lightweight data interchange format. It encodes Py
3 min read
Working With JSON Data in Python
JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which cont
6 min read
Convert JSON to CSV in Python
The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-
3 min read
Convert Text file to JSON in Python
JSON (JavaScript Object Notation) is a data-interchange format that is human-readable text and is used to transmit data, especially between web applications and servers. The JSON files will be like nested dictionaries in Python. To convert a text file into JSON, there is a json module in Python. This module comes in-built with Python standard modul
4 min read
Flattening JSON objects in Python
JSON(JavaScript Object Notation) is a data-interchange format that is human-readable text and is used to transmit data, especially between web applications and servers. The JSON files will be like nested dictionaries in Python. To convert a text file into JSON, there is a json module in Python. This module comes in-built with Python standard module
3 min read
Pretty Print JSON in Python
JSON is a javascript notation of storing and fetching the data. Data is usually stored in JSON, XML or in some other database. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to Read, Write and Parse JSON using Python Pretty Print JSON Whenever d
2 min read
Python - JSON to XML
A JSON file is a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which is a standard data interchange format. It is primarily used for transmitting data between a web application and a server.A JSON object contains data in the form of a key/value pair. The keys are strings and the values are the JSON
5 min read
Python - XML to JSON
A JSON file is a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which is a standard data interchange format. It is primarily used for transmitting data between a web application and a server. A JSON object contains data in the form of a key/value pair. The keys are strings and the values are the JSO
4 min read
Python program to extract a single value from JSON response
We will discuss how Python can be used to extract a value from a JSON response using API and JSON files. Extract value from the JSON response using the API Initially, use the API Key variable to declare the base URL. Where the first currency needs to be converted with the second, ask the user to enter a currency name and save it in a variable. The
3 min read
How to import JSON File in MongoDB using Python?
Prerequisites: MongoDB and Python, Working With JSON Data in Python MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs. JSON stands for JavaScript Object Notation. It is an open standard file format, and data int
2 min read
How to Parse Data From JSON into Python?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write for machines to parse and generate. Basically it is used to represent data in a specified format to access and work with data easily. Here we will learn, how to create and parse data from JSON and work with it. Before starting the det
2 min read
Convert JSON data Into a Custom Python Object
Let us see how to convert JSON data into a custom object in Python. Converting JSON data into a custom python object is also known as decoding or deserializing JSON data. To decode JSON data we can make use of the json.loads(), json.load() method and the object_hook parameter. The object_hook parameter is used so that, when we execute json.loads(),
2 min read
Article Tags :
Practice Tags :