JSON is a format used to transfer the data as text over a network.
You can dump a dictionary into a json file using json.dump(dict, fileobject) in Python.
Basic Example
import json
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
with open('result.json', 'w') as fp:
json.dump(yourdict, fp)
Output
{"one": 1, "two": 2, "three": 3, "four": 4, "fourr": 4}
This tutorial teaches you the different methods available to dump a dictionary into a JSON file in Python.
Table of Contents
Using JSON Dump
The dump()
method serializes the python dictionary as a JSON formatted stream and writes it into a file-like object passed in as the parameter.
Parameters
- Dictionary Object – Mandatory – The object that needs to be dumped into a file
- File Like Object – Mandatory – The file object into which the dictionary needs to be dumped
Other optional parameters can be used to customize dictionary keys, separators, and sorting of the dictionary. Refer to the doc for more details.
Code
The following code demonstrates how to serialize the python dictionary to a file object.
- The file is opened in a
write
mode asf
- The
json.dump()
method is used to dump thedict
object into thef
import json
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
with open('result.json', 'w') as f:
json.dump(yourdict, f)
Output
You’ll have the following content when you open the file result.json.
The complete dictionary content is serialized in a single line.
{"one": 1, "two": 2, "three": 3, "four": 4, "fourr": 4}
Using JSON Dump With Indent
It is easier when the file content is properly indented and formatted.
- Use the
indent
parameter to dump the dictionary with each key-value pair in a newline and proper indentation.
Writing the content of a dictionary with an indent is also known as pretty printing.
Code
import json
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
with open('result.json', 'w') as f:
json.dump(yourdict, f, indent=4)
Output
You’ll see the following content when you open the file result.json.
{
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
Sorting Keys While Dumping
Since Python 3.7, the insertion order is maintained in the dictionary. Hence, if the keys are inserted in a sorted manner, they’ll also be retrieved in the same order.
However, the older version of Python doesn’t maintain the key insertion orders.
To sort the keys while dumping them into a JSON file,
- use the
sort_keys=True
parameter.
Code
import json
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
with open('result.json', 'w') as f:
json.dump(yourdict, f, indent=4, sort_keys = True)
Output
When you open the result.json file, you’ll see that the keys are sorted and printed.
{
"four": 4,
"fourr": 4,
"one": 1,
"three": 3,
"two": 2
}
Using JSON Dumps
The dumps()
method serializes the passed object to a JSON formatted String.
- Use
dumps()
instead ofdump()
when you want to dump the dictionary as a string instead of a Stream.
Code
The following code demonstrates how to use the dumps()
method to serialize the dictionary object into a file as String
.
import json
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
with open("result.json", "w") as f:
f.write(json.dumps(yourdict))
Output
{"one": 1, "two": 2, "three": 3, "four": 4, "fourr": 4}
Using Pickle
The Pickle library is used for data serialization.
Pickle allows you to store the data in a binary format.
The pickle.drop()
method is used to serialize the dictionary into a file object. However, the dictionary content in the file will not be in a human-readable format.
Use this method when you want to write the dictionary into a binary file and want to keep the contents in a machine-readable form.
Code
import pickle
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"fourr": 4
}
with open('result.p', 'wb') as f:
pickle.dump(yourdict, f)