JSON (JavaScript Object Notation) is a popular file format that allows structured data to be easily presented and transferred between the server and the application. The structure of this file resembles some Python objects like the list, tuple and dictionary. You can use the dump() and dump() methods of the JSON module to convert any object in the dictionary into a JSON object. This article shows how to convert a dictionary object to a JSON object in Python.
Methodjson.dumps():
This method is used to convert dictionary objects to JSON data for analysis or reading, and is slower than the dump() method. You could try this out Outsource hosting support.
Syntax:
json.dumps(object, indentation=not, sort_keys=wrong)
This method has many optional arguments. The use of one mandatory and two optional arguments is presented in this article. Here, the first argument is mandatory, allowing you to enter any object in the dictionary, the second argument is used to determine the number of units for dashes, and the third argument is used to sort the keys.
Method json.dump():
This method is used to save Python objects in a JSON file. It is faster than the dumps() method because it writes to memory and to a separate file.
Syntax:
json.dump(dicionary, fileHandler, indent=None)
This method has many arguments, such as dumps(). This article uses three arguments to convert data from dictionary objects to JSON data and to store the data in a JSON file. Here, the first argument is used to accept the Dictionary object to be converted to a JSON object, and the second argument is used to accept the handler name of the file to which the JSON data is to be written. The third argument is used to specify the notch unit.
How these two methods can be used to convert a dictionary object to a JSON file or a JSON string is discussed later in this article.
Example 1: Converting the dictionary to JSON using indented landfill(()
Earlier it was mentioned that the dumps() method has a mandatory parameter and can accept a Dictionary object to convert data into a JSON string. In the following script, dict_data is a dictionary variable that contains data from a particular student directory. First, the dumps() method is used with a single argument, and the dict_data value is converted to JSON data. The output of the dictionary and JSON format is the same as when the JSON data does not use indentation. Next, the dumps() method is used with two arguments, and 3 is used as the depression value for JSON data. The second JSON output is generated with a dash.
#! /usr/bin/env python3
# import json module
import json
# Define
dict_data = { students_id: 011894, name : Matthew, party: 30, semester: 6 }
# Printing dictionary data
(Dict output: n, dict_data, n)
# Convert the dictionary to a JSON object without indentation
json_data = json.dumps(dict_data)
# Print JSON data
print(JSON output without indentation: n,json_data, n)
# convert dictionary to a JSON object with indentation
json_data = json.dumps(dict_data,indent=3)
# output json-data with indentation
(indented JSON output: n, json_data)
A way out:
After executing the script, the following output appears.
Example 2: Converting the dictionary to JSON using the dumps() function with the sort_keys
JSON data keys can be sorted with the sort_keys argument of the dumps() function. The default value of this argument is incorrect. The following script converts the Dictionary object to JSON data without using sort_keys and without using sort_keys to indicate the use of this argument. The first method dumps() is used with indentation 5, and the output shows JSON data with indentation 5. The second method dumps() uses the function sort_keys to sort key values, which is set to True. The last JSON output displays the data after the key values have been sorted.
#! /usr/bin/env python3
# import json module
import json
# Define dictionary
dict_data = {name : Madison, months. May, Year: 2020, Sales: 1000, 2100, 3500, 1200] }
# Prints dictionary data
printout (dictionary output: n, dict_data, n)
# Convert list dictionary to json
json_data = json.dumps(dict_data, dash = 5)
# Print json data
print(JSON dash: n, json_data)
# Convert a dictionary containing a list of data to JSON using the sort keys
json_data = json.dumps(dict_data,indent = 5, sort_keys = True)
# Print JSON data sorted using the keys
print(JSON output sorted indent: n, json_data)
A way out:
After executing the script, the following output appears. The first JSON output shows the core values defined in the dictionary, while the second JSON output shows the core values in sorted order.
Example 3: Convert the dictionary to JSON data and save it in JSONfile.
If you want to save the JSON dictionary data in a file after conversion, you must use the dump() method. This example shows how you can convert dictionary objects to JSON data and save the data in a JSON file. Here the dump() method uses three arguments. The first argument takes the previously defined dictionary object. The second argument accepts the file processing variable that was also previously set to create a JSON file. The third argument defines the value of the indentation. The contents of the rewritten JSON will be printed in the weekend at the latest.
#! /usr/bin/env python3
# import json module
import json
# Define dictionary
dict_data = { c-101 : PHP programming, C-102 : Bash programming, c-103:
Python programming,
c-104 : Object-oriented programming }
# Printing dictionary data
printout (Dictionary output: n, dict_data, n)
# Set the name of the json file
jsonFile = ‘course_list.json’
# Open the json file to save the json data
with open(jsonFile, ‘w’) in fileHandler1:
json.dump(dict_data, fileHandler1, dash = 2)
# Open a JSON file to read
fileHandler2 = open(jsonFile)
print(JSON file content: n, fileHandler2.read())
A way out:
After executing the script, the following output appears.
Conclusion:
To facilitate the various programming tasks, you must hide the dictionary data in the JSON data. This data conversion is important because JSON makes it easy to transfer data from one scenario to another. I hope this tutorial will help Python users learn how to convert dictionary data to JSON data and use it correctly in their script.python dict to json,python extract data from json file,python list to json,how to scrape json data python,python object to json,create a dictionary from json python,iterate json python example,python json cheat sheet