Python | Convert Dictionary to JSON

Python provides different data structures that are used to store all kinds of data. Python Dictionary and JSON (Javascript Object Notation) stores data in a well-organized way. They both store the value in “key-value” pairs form by placing inside the curly brackets “{ }”. In this article, a complete overview of Python Dictionary to JSON will be explained with numerous examples. The following topics are discussed in this Python guide:

So, let’s get started!

How to Convert a Python Dictionary to JSON?

The “json.dumps()” function of the JSON module is used to convert Python Dictionary to JSON. The syntax of the “json.dumps()” function is mentioned below:

Syntax:

json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False)

In the above syntax:

  • The parameter “obj” serializes the object as a JSON formatted stream.
  • The parameter “indent” is used to add the space or newline at the beginning of code. It increases the readability of code.
  • The parameter “skipkeys” default value is “False”; when it becomes “True” the program will automatically skip the error if the data type is not of basic type like str, int, float, etc.
  • Another important parameter “sort_keys” is used to sort the dictionary keys when it takes the Boolean value “True”. The default value of sort keys is the boolean value “False”.

Note: Only those parameters are defined above which are necessary to understand for Python conversion of Dictionary to JSON. The other parameters of this function are optional in our case.

Example 1: Simple Dictionary to JSON Conversion

In the example below, the “json.dumps()” function converts the simple dictionary value into a JSON string.

Code:

import json

dict_val = {'1':'Python', '2':'Guide', '3':'itslinuxfoss'}
json_val = json.dumps(dict_val)

print(json_val)

In the following code:

  • The “JSON” module is imported into the program.
  • The dictionary value is stored in the variable named “dict_val”.
  • The “json.dumps()” function takes the value of the input dictionary variable as a parameter. The value will return a JSON object as an output.

Output:

The above output shows the value of the JSON string.

Example 2: Nested Dictionary to JSON

Nested dictionaries can also be initialized and converted into JSON strings using the “json.dumps()” function. Let’s understand it via the following code:

Code:  

import json
dict_val = {'Boy':{"Joseph": "22","color": "white"},
 'Girl':{"Lily": "18","color": "pale"},}
json_val = json.dumps(dict_val)
print(json_val)

In the above code example:

  • The “JSON” module is imported at the start of the code.
  • The nested dictionary is initialized and stored in a variable named “dict_val”.
  • The “json.dumps()” function converts the dictionary value into JSON by taking the value of the dictionary variable inside its parentheses.

Output:

The output shows the value of nested JSON strings.

Example 3: Dictionary Sorting and Converting to JSON

The Dictionary “keys” value sort using the parameter value “sort_keys” of the “json.dumps()” function. Let’s see an example of code given below for better understanding:

Code:

import json
dict_val ={"Name": "Joseph", "Age": "22", "Gender": "Male"}
json_val = json.dumps(dict_val, indent = 3, sort_keys = True)
print(json_val)

In the following code:

  • The module named “JSON” is imported.
  • The dictionary value is defined and stored in the variable “dict_val”.
  • The “json.dumps()” function takes three parameters as an argument. The first parameter is the variable value of the dictionary and the second parameter is the indent value (Beginning space of code line). The third parameter named “sort_keys” sorts the value of the dictionary by taking the boolean value “True”.

Output:

The result shows that the Dictionary is converted into JSON.

Conclusion

In Python, the “json.dumps()” function of the JSON module converts the value of the dictionary into JSON. The dictionary value can be sorted according to “keys” and then converted into JSON. The nested dictionary also converts into JSON using the “json.dumps()” function. The JSON value became more readable using the parameter value “indent” of the “json.dumps()” function. This article has provided all the details of Python Dictionary conversion into JSON.