Updated June 28, 2023
Introduction to Python json.dumps
JSON (JavaScript Object Notation) is a text-based file format used for storing and transferring data. It is a widely supported format in various programming languages. In Python, JSON is supported through the built-in json package. To utilize JSON in Python, the json package needs to be imported. This package provides functionalities to convert Python objects into JSON string representations. The json.dumps() function is specifically used to convert a Python dictionary object into a JSON string.
The syntax is as follows:
json.dumps(python_dictionary_object)
where python_dictionary_object is the python object that is to be converted to json string representation.
Working of json.dumps() Function in Python
- The executable file made of text in a programming language that can be used to store the date and transfer the data is called JavaScript Object Notation, also called JSON.
- This JSON is supported using a built-in package called json, and to be able to use this feature in Python, the json package must be imported.
- Whenever an object in Python is converted to json string, we use a python function called json. dumps() function.
- A JSON string representation of the Python dictionary object is returned by using json.dumps() function.
Examples of Python json.dumps
Following are the examples as given below:
Example #1
Python program to convert the given Python dictionary object into json string representation:
Code:
#a package called json is imported to make use of json.dumps() function
import json
#creating a dictionary object in python which is to be converted into json string representation
pythondictionary ={1:'Learning', 2:'is', 3:'fun'}
# json.dumps() function is used to convert the given python dictionary object into json string representation
jsonstring = json.dumps(pythondictionary)
#displaying the json string representation of the given python dictionary object
print '\n'
print('The json string representation of the given python dictionary object is:')
print '\n'
print(jsonstring)
Output:
In the above program, a package called json is imported to make use of json.dumps() function to convert the given python dictionary object into json string representation. Then a dictionary object is created in python to convert it into json string representation. Then this function is used to convert the created python dictionary object onto json string representation. Then the converted json string representation is displayed as the output on the screen.
Example #2
Python program to convert the given Python dictionary object into json string representation:
Code:
#a package called json is imported to make use of json.dumps() function
import json
#creating a dictionary object in python which is to be converted into json string representation
pythondictionary = {1:'India', 2:'is',
3:'my', 4:'nation'}
# json.dumps() function is used to convert the given python dictionary object into json string representation
jsonstring = json.dumps(pythondictionary)
#displaying the json string representation of the given python dictionary object
print '\n'
print('The json string representation of the given python dictionary object is:')
print '\n'
print(jsonstring)
Output:
The mentioned program imports the json package to utilize the json.dumps() function is used to convert a given Python dictionary object into a JSON string representation. After importing the json package, a dictionary object is created in Python. This dictionary object is created to convert it into a JSON string representation.
Example #3
Python program to convert the given Python dictionary object into json string representation:
Code:
#a package called json is imported to make use of json.dumps() function
import json
#creating a dictionary object in python which is to be converted into json string representation
pythondictionary = {1:'Python', 2:'is',
3:'interesting'}
# json.dumps() function is used to convert the given python dictionary object into json string representation
jsonstring = json.dumps(pythondictionary)
#displaying the json string representation of the given python dictionary object
print '\n'
print('The json string representation of the given python dictionary object is:')
print '\n'
print(jsonstring)
Output:
The above program imports the json package to utilize the json.dumps() function converts a given Python dictionary object into a JSON string representation. Following the import, a dictionary object is created. This dictionary object is then converted into a JSON string representation using the json.dumps() function.
Example #4
Python program to convert the given Python dictionary object into json string representation:
Code:
#a package called json is imported to make use of json.dumps() function
import json
#creating a dictionary object in python which is to be converted into json string representation
pythondictionary = {1:'EDUCBA', 2:'for',
3:'learning'}
# json.dumps() function is used to convert the given python dictionary object into json string representation
jsonstring = json.dumps(pythondictionary)
#displaying the json string representation of the given python dictionary object
print '\n'
print('The json string representation of the given python dictionary object is:')
print '\n'
print(jsonstring)
Output:
The json package is imported to utilize the json in the mentioned program.dumps() function for converting a given Python dictionary object into a JSON string representation. Next, a dictionary object is created in Python to be converted into a JSON string representation. The json.dumps() function then converts the created Python dictionary object into a JSON string.
Recommended Articles
We hope that this EDUCBA information on “Python json.dumps” was beneficial to you. You can view EDUCBA’s recommended articles for more information.