</bugz loading .

How to Convert a Dictionary to JSON in Python?

What is JSON ?

JSON refers to JavaScript Object Notation. It is a light-weight data format that is used to store and transmit structured data. It is very similar to dictionaries in Python.

It is widely used to transfer data between web applications and servers. Because of its effectiveness in reducing data size, JSON is often considered suitable to store data. JSON has become a popular choice for structuring data in configuration files across various applications and platforms.

Since JSON and dictionaries in Python have a similar format, it's convenient for Python developers to work with JSON. To use JSON in Python, we should import the built-in library json.

How to convert dict to JSON in Python ?

To work with JSON data in Python, we will need to import the json module. It is a built-in module, so we don't need to install any external libraries.

We can import the json module by using the import keyword as shown below.

import json

Converting Python dict to JSON

To convert a Python dictionary to JSON, we can use the dumps() method of the json module.

Syntax

json.dumps(python_dict)

Example

import json

dict = {
        "name": "Bugzee",
        "city": "New York"
       }

json_string = json.dumps(dict) # converting the dict to json string

print(json_string)

In the above code, we are creating a simple python dictionary dict.
Then we are converting the dictionary to a json string using the json.dumps() method.

Output

{"name": "Bugzee", "city": "New York"}

Converting Python dict to JSON file

To convert a Python dictionary to JSON file i.e to convert a python dictionary to a json object and write it to a json file, we can use the dump() method of the json module.

It is very similar to the json.dumps() method. The only difference between json.dump() and the json.dumps() method is that json.dump() method requires an extra parameter to specify the file in which the json string must be written.

Syntax

json.dump(python_dict,file)

Example

import json

dict = {
        "name": "Bugzee",
        "city": "New York"
       }

# converting the dict to json string and writing it to the 'data.json' file

with open("data.json", "w") as file:
    json.dump(dict, file)

In the above code, we are creating a simple python dictionary dict.
Then we are converting the dictionary to a json string and writing it to the data.json file using the json.dump() method.

Output

{"name": "Bugzee", "city": "New York"}
# will be stored to the data.json file

Common Parameters of json.dump() and json.dumps()

json.dumps() and json.dump() methods have several useful parameters. They are used to customize the output json string.

Some of the most common parameters are :

Indent

It is used to show the hierarchy in json using spaces/tabs for better readability.

Example

import json

dict = {
        "name": "Bugzee",
        "city": "New York"
       }

with open("data.json", "w") as file:
    json.dump(dict, file, indent=4) # adding indentation of spaces

Output

{
        "name": "Bugzee",
        "city": "New York"
}
# will be saved to data.json file

In the above code, the dicitonary dict will be converted in to a json string with indentation of 4 spaces and will be saved in the data.json file.

Sort Keys

It is used to sort the keys of the json data alphabetically.

Example

import json

dict = {
        "name": "Bugzee",
        "city": "New York"
       }

json_string = json.dumps(dict, sort_keys=True) # sorting the keys alphabetically

print(json_string)

Output

{"city": "New York", "name": "Bugzee"}

Conclusion

In conclusion, we need to import the json module in order to work on JSON in python. A Python dictionary can be converted to a JSON string using the json.dumps() function, and the json.dump() function can be used to convert a python dictionary to a JSON string and save it to a JSON file. These methods offer parameters for the customization of the output JSON string such as indent, sort_keys, etc.