Python | Convert JSON to Dictionary

In any programming language, data is required to complete any work. Data is initialized in the program at the beginning of code or during execution. There are different ways to organize and store data in “Python” such as JSON, Dictionary, String, List, int, etc. The “JSON” Javascript Object Notation stores the string data in a well-organized manner. Similarly, Dictionary is similar to JSON because of both store data in the “key-value” pair format.

This article will demonstrate the method to convert JSON into a dictionary. The following content is discussed in this Python guide:

So, let’s get started!

How to Convert JSON to Dictionary?

In Python, the function “json.loads()” of the JSON module converts the data of JSON into the dictionary.  The value of the “JSON key” is in string format and stored with “double quotation marks” which is mandatory for the initialization of JSON data. The syntax of “json.loads()” is given below :

json.load(file_name)
How to install Kali Linux in Window...

The “file_name” parameter takes the value of the JSON data file and returns the value of the object in the Python Dictionary.

So, let’s begin with our first example of conversion of JSON to Dictionary.

Example 1: JSON to Dictionary

The “JSON” data can store the value in key-value pair format, and the key value must be initialized inside the double quotation marks such as {“Name”}. As we know that JSON is the standard format to store and transfer text all over the network in JavaScript language. To use JSON in Python, we need to import the JSON inbuilt package at the start of the coding program. The below code shows the conversion of JSON to Dictionary in Python:

Code:

import json

json_Str = '{"Name":"itslinuxfoss", "Guide": "python"}'
dict_val = json.loads(json_Str)

print(dict_val)
print(type(dict_val))
print(dict_val['Name'])
print(dict_val['Guide'])

In the above code:

  • The built-in package “JSON” is imported in the program.
  • The value of “JSON” is initialized and saved in the variable named “json_str”.
  • The function named “json.loads()” take the value of JSON string as an argument and return the value in dictionary format.
  • The “type()” function is used to verify the dictionary data type.
  • The value of the “key” is accessed using the key name inside the square bracket i.e,. “dict_val[‘Name’]”.

Output:

The output shows the conversion of “JSON into DIctionary”.

Example 2: Nested JSON Data to Dictionary

In the example code given below, the JSON data are stored in a nested object which means we are storing the “value of the key” as the “value of another key” in the form of a “key-value” pair. Let’s understand how we can convert and access the JSON data:

Code:

import json

json_Str = '{"Name":"itslinuxfoss", "Guide": {"Tutorial":"python"}}'
dict_val = json.loads(json_Str)
print('Dictionary Value of JSON is : ',dict_val)
print(type(dict_val))
print('\nValue of Key "Name" :',dict_val['Name'])
print('\nValue of Key "Guide" :',dict_val['Guide'])
print('\nValue of Nested Key :',dict_val['Guide']['Tutorial'])

In the above code:

  • The “JSON” module is imported in the program.
  • The JSON string with the nested object is initialized in the variable named “json_str”.
  • The key value “Guide” has a nested object with a different key-value pair named “Tutorial”.
  • The function “json.loads()” converts the JSON string into the dictionary.
  • The “type()” function is used to verify the dictionary data structure.
  • The value of keys is accessed by calling their “key” names.
  • The nested object of JSON string is accessed by calling the “key” name of the outer and inner key-value pair.

Output:

In the above output, the JSON string is converted into the dictionary, and the nested object is accessed using the key names.

That’s all from this Python guide!

Conclusion

In Python, the “json.loads()” function is used to convert the JSON data file into the dictionary. The value of the simple “key” and “nested key” can easily be accessed using the variable name of the dictionary. The Data format “JSON” and the data Structure “Dictionary” are used to store and process data in Python. The “json.loads()” function is used to convert simple and nested JSON data files very efficiently. This article has briefly explained the conversion of JSON to Dictionary in Python.