Converting dictionary to JSON in Python

In this article, we will learn the different methods for converting a dictionary to a JSON object in Python. We will convert these methods with each step along with good examples. So let’s get started by first introducing the Python Dictionary and JSON.

Python dictionary

Python comes with built-in data structures of lists, tuples, sets and most importantly dictionaries. Dictionaries store values based on a key-value pair format in which keys act as an index but are defined by the programmer and values are the corresponding data stored in dictionaries.

Dictionaries have other names like associative arrays, hash maps, and maps in different programming languages. The following are some methods that can be applied to a Python dictionary such as pop(), popitem(), clear(), and copy(). There are many more methods that can be applied to Python dictionaries because Python is considered a versatile language that can be applied to almost anything from Web Programming to Machine Learning.

JSON ( JavaScript Object Notation)

JSON which stands for JavaScript Object Notation, was produced from a subset of JavaScript language dealing with object literal syntax but nowadays it’s completely independent of JavaScript.

JSON is supported by many languages including C++, C, Python and many more. JSON is a format which is used for transferring information across clients and a web server, it is a text-based format and all of the data is organized in plain text which makes it easier for both humans and machines to understand and parse it.

Key-value pairs are used to store JSON data; the key must be of the string data type, and the values may be arrays, booleans, numbers, nulls, etc. This gives JSON flexibility and allows data exchange between many computer languages and platforms simple.

Steps to convert a dictionary to JSON :

We would be using the built-in module of pythons to convert a dictionary in Python to a JSON file. A JSON file comes in with its own extension .json which is a clear indication of the file being a JSON file. This module contains a dump() function which we will use to dump (deposit) our dictionary content to a JSON file.

Step 1: Import JSON Library

In the first step, we need to import a library known as JSON by using the Import statement:

import json

Step 2: Have a proper Python Dictionary

As an example take the below shopping_cart as a dictionary which we want to write into a JSON file as stated below:

shopping_cart = {

Step 3: Open JSON file in Context Manager

Now in this step, we have to use the with keyword because we need to open a file to write into it and then close it when we exit the given block of code.

with open('jsondata.json', 'w') as result:

The open() function is used to write the dictionary data into a JSON file. This function takes in two parameters here, the first one is the name of the file and the second is a writing mode which means how you want to write your content, in which w is for write and a is for append in case how you want your content to be written.

The append is used if you want to add some content later to the same file, the content previously written will not be deleted or erased, while in the case of writing the previous content will get erased and the newly created content will be written into it.

In this statement, the open('jsondata.json', 'w') creates a file object which is opened in write mode and that object is assigned to an alias called as the result. The as keyword is used for assigning the object to the variable result.

Step 4: Use the dump() function

In this last step, we will use a function called dump() to convert our data to JSON. This function takes in the data that needs to be converted into the JSON format which in our case is the dictionary called shopping_cart and the variable name we declared in step 3 which is known as the result. After the execution of this code, a file with the name jsondata.json will be created in the same directory you are working in, which will be the required file converted from a dictionary to JSON format.

import json
shopping_cart = {
    "Quantity": 30,
    "Color": "White"
with open('./jsondata.json', 'w') as result:
    json.dump(shopping_cart, result)

Summary and Conclusion

In this article, we have learned how to convert a Python dictionary to a JSON object in Python. We have discussed it with an explanation and an example. I hope this was helpful if you have any questions, please let me know in the comment section.

Leave a Comment

Scroll to Top
× DM Here!