How to convert dictionary to JSON in Python

In this Python tutorial, we will learn how to Convert dictionary to JSON in python and also we will cover these topics:

  • How to convert nested dictionary to JSON in Python
  • How to convert a dictionary to JSON string in Python
  • How to convert a dictionary to JSON file in Python
  • Python dictionary to JSON quotes
  • Convert dictionary to JSON array in python
  • Convert multiple dictionaries to JSON python
  • Convert dictionary to JSON using sort_key python

You may like Working with JSON data in Python and Python Concatenate Dictionary

Convert dictionary to JSON Python

Here, we can see how to convert dictionary to Json in python.

  • In this example, I have imported a module called json and declared a variable as a dictionary, and assigned key and value pair.
  • Another variable details is declared to store the dictionary into json using json.dumps(), and used indent = 5. The indentation refers to space at the beginning of the code line.

Example:

import json 
dictionary ={"Name": "Kushi", "Branch": "CS", "CGPA": "9.4"} 
details = json.dumps(dictionary, indent = 5) 
print(details) 

The dumped dictionary can be seen in the output. You can refer to the below screenshot for the output.

Convert dictionary to JSON python
Convert dictionary to JSON python

This is how to convert dictionary to JSON in Python.

Convert nested dictionary to JSON python

Here, we can see how to convert nested dictionary to Json in python.

  • In this example, I have imported a module called json and declared a variable as a dictionary, and assigned key and value pair. Here, I have taken two dictionaries as fruit and vegetable.
  • Another variable details is declared to store the dictionary into json using json.dumps(), and used indent = 5. The indentation refers to space at the beginning of the code line.

Example:

import json 
dictionary = {
 "fruit":{"Apple": "1","color": "red"},
"vegetable": {"tomato": "4","color": "red"},
}
details = json.dumps(dictionary)
print(details) 

The output will be in the nested dictionary format below screenshot shows the output.

Convert nested dictionary to JSON python
Convert nested dictionary to JSON python

Convert dictionary to JSON string python

Now, we can how to Convert dictionary to JSON string in python.

  • In this example, I have imported a module called json and declared a Python dictionary as a student, and json.dumps() is used to store the dictionary. The indent = 2 is used to get the space in the lines of the code.
  • Here, I have used two print statements one without indent and one with an indent.

Example:

import json
student = {"Name": "Kushi","Age": 5,"hobby":"playing"}
print(json.dumps(student))
print(json.dumps(student, indent = 2))

Two string can be seen in the output one with indent and another without indent. Below screenshot shows the output.

Convert dictionary to json string python
Convert dictionary to json string python

Convert dictionary to JSON file Python

Here, we can see how to Convert dictionary to JSON file in python.

  • In this example, I have imported a module called json, and declared a dictionary as a student and to open the json file I have used with open(‘data.json’, ‘w’) as f.
  • The data.json is the name of the file and the ‘w’ mode is used to write the file. To store the file json.dump() is used to store the dictionary.
  • To open the json file first, we have to check the location of the file and then we have to open it in the visual studio code by giving the filename.

Example:

import json
student = {"Name": "Kushi","Age": 5,"hobby":"playing"}
with open('data.json', 'w') as f:
    json.dump(student, f)

Below screenshot shows the output value which is stored in the json file in visual studio code.

Convert dictionary to JSON file python
Convert dictionary to JSON file python

This is how to covert dictionary to JSON file in Python.

Python convert dictionary to JSON quotes

Here, we can how to convert dictionary to JSON quotes in python.

  • In this example, I have imported a module called json.
  • The class fruit is declared and defined as def__str__(self).
  • The __str__(self) method used for the string representation, and a variable called combo is declared, and the quote variable is assigned with class fruits.
  • The combo parameter is passed, to get the output I have used print(quote).

Example:


import json
class fruits(dict):
    def __str__(self):
        return json.dumps(self)
combo = [['mango','orange']]
quote = fruits(combo)
print(quote)

We can see th output as key value pair with double quotes. You can refer to below screenshot for the output.

Python dictionary to JSON quotes
Python dictionary to JSON quotes

This is how to convert dictionary to JSON quotes in Python.

Convert dictionary to JSON array Python

Now, we can see how to Convert dictionary to JSON array in python.

  • In this example, I have imported a module called json.
  • The variable as dictionary and assigned key-value pair.
  • Another variable is declared as an array and this is used to check which is key and which is value. The for loop is used to store the value, whereas the json.dumps() is used to store the dictionary.

Example:

import json
dictionary = {'kitkat': 3, '5star': 1}
array = [ {'key' : k, 'value' : dictionary[k]} for k in dictionary]
print(json.dumps(array))

In the output, we can clearly see which is key and which is value. You can refer to the below screenshot for the output.

Convert dictionary to JSON array in python
Convert dictionary to JSON array in python

This is how to convert dictionary to JSON array in Python.

Convert multiple dictionaries to JSON Python

Here, we can see how to convert multiple dictionary to json in python.

  • In this example, I have imported a module called json, and declared a variable as a dictionary.
  • And then we assigned multiple dictionaries to it, and used json.dumps() to store the dictionary, to get the output I have used print(details).

Example:

import json 
dictionary = {"fruit":{"Apple": "1","color": "red"},"vegetable": {"tomato": "4","color": "red"},"chocolate":{"kitkat":"2","color":"red"}}
details = json.dumps(dictionary)
print(details) 

Below screenshot shows the output.

Convert multiple dictionary to JSON python
Convert multiple dictionary to JSON python

This is how to convert multiple dictionaries to JSON in Python.

Convert dictionary to JSON using sort_keys Python

Now, we can see how to convert dictionary to JSON using sort_keys in python.

  • In this example, I have imported a module called json, and declared a variable called the dictionary, and assigned key and value pair.
  • To store the value I have used json.dumps and also indent = 5 is used. The sort_keys = true is used to sort the dictionary, if we give False to the value, then the dictionary will not be sorted.

Example:

import json 
dictionary ={"Name": "Kushi", "Branch": "CS", "CGPA": "9.4"} 
details = json.dumps(dictionary, indent = 5, sort_keys = True) 
print(details) 

We can see the sorted dictionary as the output in the below screenshot.

Convert dictionary to JSON using sort_keys python
Convert dictionary to JSON using sort_keys python

This is how to convert dictionary to JSON using sort_keys in Python.

You may like the following Python tutorials:

In this tutorial, we have learned about how Convert dictionary to JSON in Python, and also we have covered these topics:

  • Convert nested dictionary to JSON python
  • Convert dictionary to JSON string python
  • Convert dictionary to JSON file python
  • Python dictionary to JSON quotes
  • Convert dictionary to JSON array in python
  • Convert multiple dictionaries to JSON python
  • Convert dictionary to JSON using sort_key python