convert json to dict in python

How to Convert JSON to Dict in Python

Python is a powerful programming language that allows you to work with different types of data. Sometimes you may need to convert one type of data into another. For example, you may need to convert JSON data into a python dictionary. In this article, we will look at how to convert JSON to Dict in Python.


How to Convert JSON to Dict in Python

Although JSON and dictionary look similar they are different data types and using one in place of the other can result in errors. JSON is a string while dictionary is an object. So JSON data is within quotes while python dict is a data structure. Accordingly, they both have different set of member functions and operators available.

Here are the steps to convert JSON to Dict in Python. Python comes with an in-built json module that you can use to convert JSON data into python dictionary. It provides load() and loads() function to easily convert JSON to dictionary. load() is used to for JSON file and loads() is used for JSON string.

In this article, we will look at how to convert both JSON string as well as JSON file into python dict.


Convert JSON String to Dict

Let us say you have the following JSON string variable b. In the following code, we use json.loads() function to convert JSON string to dict object and store the result in variable c. We use type() function to display the data type of both b & c variables.

>>> import json
>>> b='{"name":"John"}'
>>> type(b)
<type 'str'>
>>> c=json.loads(b)
>>> c
{u'name': u'John'}
>>> type(c)
<type 'dict'>

You will see that the data type of JSON string b is shown as str (for string) while that of variable c is shown as dict (for python dictionary).


Convert JSON File to Dict

Let us say you have a json file data.json and want to convert it into dict variable. Here is the code to do it.

# importing the module
import json
  
# Opening JSON file
with open('data.json') as json_file:
    data = json.load(json_file)
  
    print type(data)

In the above code, we first import json module. Then we use open function to open JSON file. Then we directly call json.load() function to convert JSON file into python dict. On running the above code, you will see the following output.

<type 'dict'>

Please note, json.loads() function is used to convert JSON string into python dict, and json.load() function is used to convert JSON file into python dict. If you use one in place of the other, you will get an error message. So please be careful with the function you use, while converting JSON to python dict.

That’s it. In this article, we have learnt how to easily convert JSON to python dict. We have seen how to convert JSON string as well as JSON file into python dict. You can use either of these approaches, depending on your requirement.

Also read:

How to Install NTP Server in RHEL/CentOS
How to Enable & Disable Root User in Ubuntu
How to Check RAM Size in Linux
How to Disable SELinux in CentOS
MySQL Row Number & Its Uses

Leave a Reply

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