How to Convert Python Dictionary to JSON


A dictionary in Python is a collection/composition of key-value pairs. It is an unordered, mutable data type used to store and retrieve data effectively. While JSON is a lightweight, standard file-interchange format that humans and machines can read and write. In Python, to convert a dictionary to JSON, the “json.dumps()” and “json.dump()” functions are used.

This post will describe different methods for the dictionary to JSON conversion in python.

There are several reasons why you might want to convert a dictionary to JSON:

  • Transfer data between applications: If you need to send data from one application to another, JSON is a convenient format because many programming languages can easily convert it to and from a dictionary.
  • Share data with other users: JSON is a widely used format for sharing data, so if you need to share data with someone else, converting it to JSON can make it easier for them to work with the data.
  • Store data in a file or database: JSON is a widely used format for storing data in a file or database because it is easy to read and write and can be easily parsed by many programming languages.
  • Make data more portable: JSON is a widely supported format, so converting data to JSON makes it more portable and easier to work within different systems.

Generally, the need for a dictionary to JSON conversion often arises when working with data that needs to be transferred or stored in a specific format or when you need to share data with others.

How to Convert a Python Dictionary to JSON?

This section will explain how to use the “json.dumps()” and “json.dump()” functions to convert a dictionary to JSON in Python.

Using json.dumps()

In Python, the json.dumps() function belongs to Python’s JSON module that can be used to convert a Python dictionary into a JSON string. For this purpose, firstly, users need to import the JSON module in the python program.

Syntax:

Here’s the syntax of using json.dumps() function to convert a Python dictionary into a JSON string:

json.dumps(dic, ind)

The parameter “dic” represents a dictionary to be converted, while the parameter “ind” represents the number of units for indentation.

Example: How to Use the json.dumps() in Python?

Here are the steps for converting a Python dictionary into a JSON string:

Step 1: Import the json module.

import json

Step 2: Create a dictionary.

example_dict = {
    "name": "Steave Smith",
    "age": 35,
    "designation": "Network Administrator"
}

Step 3: Convert dictionary to JSON

example_json = json.dumps(example_dict)

Step 4: Print JSON string

print(example_json)
Complete Code Snippet:
convert python input
Output:
convert python output

Example: How to Use the json.dumps() With Indent Parameter in Python?

Use the json.dumps() function along with the indent parameter to format the JSON string in a more readable format:

example_json = json.dumps(example_dict, indent = 2)
Complete Code Snippet:
convert python input
Output:
convert python dictionary output

Note: To sort the JSON string alphabetically, you must pass the “sort_keys=True” as an argument to the json.dumps() function.

Using json.dump()

In Python, the json.dump() function allows us to serialize a Python object as a JSON object and write it to a file.

json.dump(obj, file_pointer)

The “obj” parameter represents an object to serialize, a list, a dictionary, or any other JSON-serializable object. And a “file_pointer” parameter represents a file-like object, such as a file handle or a StringIO object, to which the serialized JSON object will be written.

Example: How to Use the json.dump() in Python?

Here are the steps for converting a Python dictionary into a JSON string using the json.dump() function:

Step 1: Import JSON module.

import json

Step 2: Create a dictionary.

example_dict = {
    "name": "Steave Smith",
    "age": 35,
    "designation": "Network Administrator"
}

Step 3: Create a file with write mode.

With open('exampleFile.json', 'w') as f:

Step 4: Convert the dictionary to JSON.

json.dump(example_dict, f)
Complete Code Snippet:
input
Output:
output

Conclusion

In Python, the “json.dumps()” and “json.dump()” functions are used to convert a dictionary to JSON. The need for a dictionary to JSON conversion often arises when working with data that needs to be transferred or stored in a specific format or when you need to share data with others. This blog explained several approaches with examples to convert a python dictionary to JSON string.

Print Friendly, PDF & Email