json.dumps() method:
This method is used to convert the dictionary object into JSON data for parsing or reading and it is slower than dump() method.
Syntax:
This method has many optional arguments. The uses of one mandatory and two optional arguments are shown in this article. Here, the first argument is a mandatory argument that is used to take any dictionary object, the second argument is used to define the number of units for indentation and the third argument is used to sort keys.
json.dump() method:
This method is used to store python object into a JSON file. It is faster than the dumps() method because it writes in the memory and the file separately.
Syntax:
This method has many arguments like dumps(). The uses of three arguments are used in this article to convert the data of a dictionary object into JSON data and store the data into a JSON file. Here, the first argument is used to take a dictionary object that needs to convert into a JSON object, and the second argument is used to take the name of the file handler of the file where the JSON data will be written. The third argument is used to set the indentation unit.
How these two methods can be used to convert dictionary object into a JSON file or JSON string are shown below of this article.
Example-1: Convert dictionary into JSON using dumps() with indentation
It is mentioned before that dumps() method has one mandatory parameter and it can take the dictionary object to convert the data into JSON string. In the following script, dict_data is a dictionary variable that contains the data of a particular student record. At first, the dumps() method is used with one argument and the value of dict_data is converted into JSON data. The output of both dictionary and JSON format is the same if no indentation is used in JSON data. Next, the dumps() method is used with two arguments, and 3 is used as an indentation value for JSON data. The second JSON output will generate with indentation.
# Import json module
import json
# Define a dictionary
dict_data = { "student_id": "011894", "name": "Matthew", "batch": 30, "semester":6 }
# Print dictionary data
print("Dictonary Output: \n", dict_data, "\n")
# Convert dictionary into json object without indentation
json_data = json.dumps(dict_data)
# print json data
print("JSON Output without indentation: \n",json_data, "\n")
# Convert dictionary into json object with indentation
json_data = json.dumps(dict_data,indent=3)
# print json data with indentation
print("JSON Output with indentation: \n", json_data)
Output:
The following output will appear after running the script.
Example-2: Convert dictionary into JSON using dumps() with sort_keys
The keys of JSON data can be sorted by using sort_keys argument of dumps(). The default value of this argument is False. In the following script, the dictionary object is converted into JSON data without using sort_keys and using sort_keys to display the use of this argument. The first dumps() method is used with indent value 5 and The output shows JSON data using indentation 5. In the second dumps() method, sort_keys is used and set to True for sorting the key values. The last JSON output will show the data after sorting key values.
# Import json module
import json
# Define a dictionary
dict_data = {'name':'Madison','Month':'May','year':2020,'sales':[1000, 2100, 3500, 1200]}
# Print dictionary data
print("Dictonary Output: \n", dict_data, "\n")
# Convert a dictionary with list data into json
json_data = json.dumps(dict_data,indent = 5)
# print json data
print("JSON Output with indentation: \n", json_data)
# Convert a dictionary with list data into json by sorting keys
json_data = json.dumps(dict_data,indent = 5, sort_keys = True)
# print sorted json data based on keys
print("Sorted JSON Output with indentation: \n", json_data)
Output:
The following output will appear after running the script. The first JSON output shows the key values as defined in the dictionary and the second JSON output shows the key values in sorted order.
Example-3: Convert dictionary into JSON data and store into a JSON file
If you want to store the JSON data into a file after converting from the dictionary then you have to use the dump() method. How you can convert a dictionary object into a JSON data and store the data in a JSON file is shown in this example. Here, the dump() method uses three arguments. The first argument takes the dictionary object that is defined before. The second argument takes the file handler variable that is also defined before to create a JSON file. The third argument defines the indentation value. The content of the newly written JSON will be printed later as output.
# Import json module
import json
# Define a dictionary
dict_data = { 'c-101': 'PHP Programming', 'c-102': 'Bash Programming', 'c-103':
'Python Programming',
'c-104': 'Object Oriented Programming' }
# Print dictionary data
print("Dictonary Output: \n", dict_data, "\n")
# Set the json filename
jsonFile = 'course_list.json'
# Open a json file for writing json data
with open(jsonFile, 'w') as fileHandler1:
json.dump(dict_data, fileHandler1, indent = 2)
# Open a json file for reading
fileHandler2 = open(jsonFile)
print("The content of the JSON file: \n", fileHandler2.read())
Output:
The following output will appear after running the script.
Conclusion:
It is required to covert dictionary data into JSON data to make various programming tasks easier. This data conversion is important because the data can transfer from one script to another script easily by using JSON. I hope, this tutorial will help the python users to know the ways to convert dictionary data into JSON data and apply them properly in their script.