The Wayback Machine - https://web.archive.org/web/20250113144529/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
Summarize
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.

Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg