How to convert JSON to a dictionary in Python?

Convert JSON String Into Dictionary

Hello folks! In this tutorial, we are going to discuss how we can convert JSON to a dictionary in Python.


What is JSON?

JSON stands for JavaScript Object Notation. It is one of the most popular and widely accepted data formats to represent structured data. It is a lightweight format used to store and exchange textual data written in JavaScript notation. The file containing the JSON data must be saved with the file extension .json.

JSON in Python

The representation of JSON data present inside a JSON file is similar to a Python dictionary. It means that the JSON data is also a collection of name: value pairs just like a Python dictionary.

In Python, we have a built-in module called json. Let’s import the json module in our Python program to work with the JSON data.

Prerequisites to convert JSON to a dictionary

  • Import the Python json module.
  • Provide the full path of the JSON file if it is not present in the same directory
  • All the JSON data (string) should be enclosed in double quotes to avoid the JSONDecodeError.

Create a sample JSON file

Let’s create a sample JSON file that will contain some JSON strings. We will be using this JSON file in our Python program to demonstrate the working of json module to handle the JSON data in Python.

{
    "Linux": ["Ubuntu", "Fedora", "CentOS", "Linux Mint", 
              "Debian", "Kali Linux"],
    "Windows": ["Windows 2000", "Windows XP", "Windows Vista", 
                "Windows 7", "Windows 8", "Windows 10"],
    "MacOS": ["OS X 10.8", "OS X 10.9", "OS X 10.10", "OS X 10.11",
              "MacOS 10.12", "MacOS 10.13", "MacOS 10.14"]
}

Convert JSON to a dictionary

We have created a sample JSON file containing JSON data (string). Now, let’s convert this JSON data into a Python object.We will follow the steps given below to convert JSON to a dictionary in Python

  1. Import the json module in the program.
  2. Open the sample JSON file which we created above.
  3. Convert the file data into dictionary using json.load() function.
  4. Check the type of the value returned by the json.load() function.
  5. Print the key: value pairs inside the Python dictionary using a for loop.
  6. Close the opened sample JSON file so that it doesn’t get tampered.

Let’s implement all these steps through Python code.

# Import json Python module
import json

# Open the sample JSON file
# Using the open() function
file = open("C:\path\sample_file.json", 'r')

# Convert the JSON data into Python object
# Here it is a dictionary
json_data = json.load(file)

# Check the type of the Python object
# Using type() function 
print(type(json_data))

# Iterate through the dictionary
# And print the key: value pairs
for key, value in json_data.items():
    print(f"\nKey: {key}")
    print(f"Value: {value}\n")

# Close the opened sample JSON file
# Using close() function
file.close()

Output:

<class 'dict'>

Key: Linux
Value: ['Ubuntu', 'Fedora', 'CentOS', 'Linux Mint', 'Debian', 'Kali Linux']


Key: Windows
Value: ['Windows 2000', 'Windows XP', 'Windows Vista', 'Windows 7', 'Windows 8', 'Windows 10']


Key: MacOS
Value: ['OS X 10.8', 'OS X 10.9', 'OS X 10.10', 'OS X 10.11', 'MacOS 10.12', 'MacOS 10.13', 'MacOS 10.14']

Summing-up

In this tutorial, we have learned how to read a JSON file and then convert it into a Python dictionary using json.load() function. Hope this topic is clear to you and you are ready to perform these operations on your own. Thanks for reading this article and stay tuned with us for more amazing content on Python programming.