Introduction to Python json.dumps
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 which is also called JSON. And this JSON is supported in python using a built in a package called json and to be able to use this feature. The json package must be imported and whenever an object in python must be converted to json string, we make use of a function in python called json.dumps() function, and a JSON string representation of the python dictionary object is returned by making use of json.dumps() function.
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
Working of json.dumps() function in Python is as follows:
- 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 which is 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 make use of a function in python called json.dumps() function.
- A JSON string representation of the python dictionary object is returned by making use of json.dumps() function.
Examples of Python json.dumps
Following are the examples as given below:
Example #1
Python program to demonstrate json.dumps() functionto 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 json.dumps() 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 demonstrate json.dumps() function 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:
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 to convert it into json string representation. Then json.dumps() 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 #3
Python program to demonstrate json.dumps() function 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:
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 to convert it into json string representation. Then json.dumps() 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 #4
Python program to demonstrate json.dumps() function 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:
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 json.dumps() 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.
Recommended Articles
This is a guide to Python json.dumps. Here we also discuss the introduction and working of json.dumps() function in python along with a different example and its code implementation. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses