Python Nested Dictionary to JSON

I will show you how to convert a Python nested dictionary to JSON format in this Python tutorial.

The process of converting a Python object into JSON is called Serialization. I created a website using Django, where I had to accept user data through a form and send it to a back-end server for further processing.

Data was being sent to the server through API, so user data was in the dictionary, and we couldn’t send the dictionary over the network. JSON is the format that you can use to send data over the network.

So, in Python, I used the Python module ‘json’ function to convert the user data into JSON format and send it to the back-end server.

In this tutorial, I have explained how you can apply the concepts of serialization or convert the dictionary or nested dictionary into a JSON-formatted string.

Python Nested Dictionary to JSON

First, a dictionary in Python is a collection of key-value pairs, where each key is unique and has a value of any data type; even the value can be another dictionary. So, when a dictionary contains another dictionary, then this concept is called a nested dictionary.

For example, this is the dictionary {‘name’:’Caleb’, ‘age’:34′}, to make this nested dictionary, add the key with value as a dictionary like this {‘name’:’Caleb’, ‘age’:34, address:{‘zipcode’:23456, ‘city’:’chicago’}}.

Second, you must convert these dictionary entries into JSON for JavaScript Object Notation. It is the format in which data are stored and exchanged.

READ:  np.add.at() function in Python [3 examples]

Convert Python Dictionary to JSON

Let’s take a simple dictionary and convert it into JSON.

user = {'name':'Smith','age':40, 'hobby':'riding'}

To convert the dictionary ‘user’ into JSON format, you must use the module ‘json’, which provides methods for encoding and decoding JSON data.

This ‘json’ module is part of the Python standard library, so you must import it in your environment, as shown below.

import json

The module ‘json’ has method dumps() that you convert the given Python object into a JSON formatted string.

So, to convert the dictionary ‘user’ into JSON, pass this dictionary to the dumps() function as shown below.

dict_to_json = json.dumps(user)
Python Dictionary to Json

Look at the output; it converts the given dictionary ‘user’ into JSON format and stores it in variable dict_to_json, as a result, returns the {“name”: “Smith”, “age”: 40, “hobby”: “riding”}.

The ‘user’ dictionary is not nested, but now you understand how to convert the dictionary to JSON using the json.dumps() function.

Next, let’s see how to convert Python nested dict to json.

Python Nested Dict to JSON

As shown below, modify the ‘user’ dictionary to create a nested dictionary.

user = {'name':'Smith','age':40, 'preferences':{'music':'classical', 'food':'American'}}

Now pass the nested dictionary ‘user’ to json.dumps() as shown in the code below.

import json

nested_dict_to_json = json.dumps(user)

print(nested_dict_to_json)
Python Nested Dict to JSON

Look at the output, the json.dumps(user) function also converted the nested dictionary ‘user’ into JSON data.

The function json.dumps() can quickly convert more complex nested dictionaries into JSON format. You have to pass the dictionary to the function json.dumps(), and that’s it.

This is how you can use the json.dump() function in Python converts the given dictionary objects into JSON objects.

READ:  How to add dropdown navbar in Django

Conclusion

In this Python tutorial, you learned how to convert a Python nested dictionary to json using the dumps() function of the module ‘json’ in Python.

You may like to read: