Updated June 16, 2023
Definition of Python Dump
Python supports the json package, which allows for the execution of script files containing textual programming code. This package enables the transfer and storage of data by utilizing the functions provided by the json module. The dump function in json supports the code scripted in key-value pairs similar to the Python dictionary that is within curly brackets. The dumps function is mainly used when we want to store and transfer Python objects, and json package allows us to perform the operation efficiently.
Syntax
The Python dump function is used by importing packages like json and Pickle in Python, and the basic syntax for both functions is
json.dump(object, skipkeys=False, ensure_ascii=True, indent=None, allow_nan=True, number_mode = None, datetime_mode = None, separators=None)
pickle.dump(Object, Destination, pickle_protocol=None, )
- json.dump represents the function that encodes and stores the Python object value into json value.
- object is the filename of the input data or a value that we are passing through the dump function.
- skipkeys is a parameter where we will declare Boolean values whether we want to skip the invalid dictionary keys.
- ensure_ascii is a parameter where we will declare the Boolean values to ensure the output should contain ASCII values or not.
- allow_nan is also a Boolean parameter that is used to allow null values.
- number_mode & datetime_mode allow us to handle the type of behaviors we handle inside the function, and datetime mode allows us to handle to format of data and time instances.
- The value we give first denotes the separation of a key-value pair from another key-value pair. 2nd value we give denotes the symbol which separates keys from their values.
For pickle package,
- The object is the Python object we have created to be pickled
- The destination is the file or data where the pickled python objected is written
- Pickle_protocol refers to the version of the pickle protocol. By default, it assigns to the Python version.
How does Python Dump Function Work?
Let us discuss a basic example of how the json dump function works.
Example #1
Code:
import json
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Converting output to json format
pets_data = open("pet_data.json", "w")
json.dump(dict_pets, pets_data)
Output:
In this example, we have created a Python dictionary with three key-value pairs, and we have converted the Python dictionary to json file format using the json package. Then, we pass the dictionary variable to the json.dump function, which serializes the Python object and writes the JSON output to the pets_data file. The json.dump function requires two positional arguments: dict_pets represents the Python object to be serialized, and pets_data is the file where the JSON output is stored or written.
Example #2
In this example, we’ll discuss the package called Pickle in Python, which helps us to serialize the Python object.
Code:
import pickle
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Serializing output using pickle
pets_data = open("pet_data.pickle", "wb")
pickle.dump(dict_pets, pets_data)
Output:
Similar to json, we have dumped the Python object using the pickle package, which is s very specific library in Python where we can serialize the Python object by using pickle.dump() function. We have declared three Python dictionaries and tried to dump the dictionary object in pickle format. “wb” is the parameter we have used in the pickle function, which is open to writing mode and binary mode.
Example #3
Let’s discuss another example where we use the json dumps() function, which is similar to the dump() function but the dumps() function allows us to convert the Python dictionary object to a string file in json format.
Code:
import json
import json
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Converting output to json format
json_dict = json.dumps(dict_pets)
print(json_dict)
Output:
Similar to the 1st example, we have created the Python dictionary with the same three key-value pairs. Here, we pass only one positional argument to the dumps() function, unlike json.dump(), which requires two positional arguments.
Since we are converting the Python object to json string format, we only require the object variable.
Example #4
In this example, we utilize the allow_nan parameter, which we discussed earlier, to handle NaN (Not a Number) values in a Python dictionary.
Code:
import json
import json
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Converting output to json format
json_dict = json.dumps(dict_pets)
print(json_dict)
Output:
When we declare the allow_nan parameter as True
import json
# python dictionary which should be dumped
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom",
"life expectency": 20
},
"Hamster": {
"Species": "golden hamster",
"country": "Turkey",
"life expectency": float("nan")
}
}
## Converting output to json format
pets_data = open("pet_data.json", "w")
json.dump(dict_pets, pets_data, allow_nan=True)
Output:
we can see from two codes that when we set the allow_nan parameter as True when our object has Nan values, we can dump the object to json output without any problem.
Conclusion
In this article, we have discussed the Python dump function in detail using various examples to get a clear understanding of the json dump function and its uses. We have also discussed the pickle package and dumps() function along with the examples, and we have discussed the usage of allow_nan parameters with an example. I hope this article helps.
Recommended Articles
This is a guide to Python Dump. Here we also discuss the definition and how the Python dump function works, along with different examples and its code implementation. You may also have a look at the following articles to learn more –