How To Dump A Dictionary Into A JSON File in Python – Definitive Guide

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.

This tutorial teaches you the different methods available to dump a dictionary into a JSON file in Python.

If You’re in Hurry…

You can use the json.dump() method to dump a dictionary into a JSON file in Python, as demonstrated in the following code.

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}

If You Want to Understand Details, Read on…

There are different methods available to dump a python dict to a JSON. Each method is explained below.

Using JSON Dump

This section teaches you how to use the json.dump() method to dump a Python dictionary into a JSON file.

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 ObjectMandatory – The object that needs to be dumped into a file
  • File Like ObjectMandatory – 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 as f
  • The json.dump() method is used to dump the dict object into the f
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.

You can 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

The following code demonstrates how to write a dictionary into a json file with indentation.

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

This section teaches you how to sort keys while dumping the dictionary into a file.

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, you can use the sort_keys=True parameter.

Code

The following code demonstrates how to sort the dictionary keys while dumping them into a JSON file.

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

This section teaches you how to use the json.dumps() method.

The dumps() method serializes the passed object to a JSON formatted String.

You can use dumps() instead of dump() 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

This section teaches you how to use the pickle library to dump the dictionary into a file. Pickle is also one of the famous libraries 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.

You can 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

The following code demonstrates how to use the pickle library to the dictionary content into a binary file.

import pickle

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

with open('result.p', 'wb') as f:
    pickle.dump(yourdict, f)

Conclusion

You’ve learned how to dump a dictionary into a JSON File in Python.

Additionally, you’ve learned how to use the indent and sort_keys while dumping the dictionary into a file.

If you have any questions, please feel free to comment below.

You May Also Like

Leave a Comment