How to convert Dictionary into JSON in Python?

Curry In Python (1)

In this article let’s learn how to convert a python dictionary into JSON. Let us first understand what JSON is. JSON stands for javascript object notation. It is generally used to exchange information between web clients and web servers. The structure of JSON is similar to that of a dictionary in python. The conditions are, that the JSON key must always be a string with double quotation marks. And, the value corresponding to the key could be of any data type, like string, integer, nested JSON, etc. The return type of JSON is the ‘string’ object type.

Also read: How to convert JSON to a dictionary in Python?

Example:

import json

a =  '{ "One":"A", "Two":"B", "Three":"C"}'

Dictionary in python is a built-in data type used to store data in a key associated with the value format. The data stored in the dictionary is unordered, unique pairs (keys are always unique, value can be repeated), and mutable. The return type of dictionary is the ‘dict’ object type.

Example:

#Dictionary in python is built-in datatype so 
#no need to import anything.

dict1 = { 'One' : 1, 'Two' : 2, 'C': 3}

Covert Dict to JSON

Python does have a default module called “json” that helps us to convert different data forms into JSON. The function we are using today is The json. dumps() method allows us to convert a python object (in this case dictionary) into an equivalent JSON object.

  • First, we import json module
  • Assign a variable name to the dictionary that has to be converted into a JSON string.
  • Use json.dumps(variable) to convert

Note: Do not get confused between json.dumps and json.dump. json.dumps() is a method that can convert a Python object into a JSON string, whereas json.dump() is a method that is used for writing/dumping JSON into a file.

Syntax of json

json.dumps(dict,intend)
  • dict – The python dictionary we need to convert
  • intend – Number of indentations (the space at the beginning of the code line)
import json

dict1 ={ 
  "Name": "Adam", 
  "Roll No": "1", 
  "Class": "Python"
} 
       
json_object = json.dumps(dict1, indent = 3) 
print(json_object)

Output:

{
   "Name": "Adam",
   "Roll No": "1",
   "Class": "Python"
}

Convert dictionary to JSON using sort_keys attribute

Using the sort_key attribute in the previously discussed dumps() method returns a JSON object in a sorted fashion. If the attribute is set as TRUE, then the dictionary is sorted and converted into a JSON object. If it is set as FALSE, then the dict has converted the way it is without sorting.

import json

dict1 ={ 
  "Adam": 1,
  "Olive" : 4, 
  "Malcom": 3,
   "Anh": 2, 
} 
       
json_object = json.dumps(dict1, indent = 3, sort_keys = True) 
print(json_object)

Output:

{
   "Adam": 1,
   "Anh": 2,
   "Malcom": 3,
   "Olive": 4
}

Convert nested dict into JSON

A dict declared inside a dict is known as a nested dict. the dumps() method can also convert such nested dict into json.

dict1 ={ 
  "Adam": {"Age" : 32, "Height" : 6.2},
  "Malcom" : {"Age" : 26, "Height" : 5.8},
}

json_object = json.dumps(dict1, indent = 3, sort_keys = True) 
print(json_object)

Output:

{
   "Adam": {
      "Age": 32,
      "Height": 6.2
   },
   "Malcom": {
      "Age": 26,
      "Height": 5.8
   }
}

Summary

In this article, we’ve discussed how to convert a dictionary data structure to JSON for further processing. We use the json module to serialize the dictionary into JSON.

References