Convert JSON String To Python Dictionary

In this tutorial, we will learn how to convert the JSON (JavaScript Object Notation) string to the Python dictionary.

JSON is a popular data format used for data manipulation. To learn creating a dictionary from JSON carry on reading this article…

Python program to convert JSON string to Dictionary

The first thing we need to do is to import the ‘json’ library as shown below.

import json

The json module used to work with JSON formatted data. We are going to use this module to do our task as we mentioned above.

The ‘json’ library has a method ‘loads’ that converts the JSON String to the Python dictionary. See the following few lines of the program.

import json
json_string='{"website":"codespeedy","topic":"json and python","year":2019,"list":[10,20,30]}'
print (my_dict)

Now if we execute the above code, it will give the output given below:

{'website': 'codespeedy', 'topic': 'json and python', 'year': 2019, 'list': [10, 20, 30]}

In the above code, we first have declared a JSON String in the “json_string” variable. After that, we have converted it into the Python dictionary using the ‘loads’ method and have stored the result in my_dict variable. Here you can see that the loads method from the json module is playing an important role.

Parse a JSON File

First, create a JSON file as shown below and save the file as ‘json_string.json’.

{"website":"codespeedy","topic":"json and python","year":2019,"list":[10,20,30]}

Now to parse the JSON file and to convert the JSON String into the dictionary, we should use the ‘load’ method instead of ‘loads’.

import json
with open('json_string.json','r') as string:

Our output is given below:

{'website': 'codespeedy', 'topic': 'json and python', 'year': 2019, 'list': [10, 20, 30]}

From the output of our above program, we can see that we are able to do our tasks. The output is a Python dictionary.

You may also read-


One response to “Convert JSON String To Python Dictionary”

  1. RAZOR says:

    What application do You use to import the files and do this commands?

Leave a Reply

Your email address will not be published. Required fields are marked *