Signup/Sign In

How to Convert a JSON String to Python Dictionary?

In this article, we will learn how to convert a JSON string to dictionary in Python. We will use built-in function available in Python for JSON and some related custom examples as well. Let's first have a quick look over the full form of JSON, and how JSON files are used.

What is JSON?

JSON stands for JavaScript Object Notation. It is a popular data format used for representing structured data. It is a lightweight format that is used for data interchanging. The data representation in JSON is similar to that of Python Dictionary. It is a collection of name/value pairs. In JSON, it is common to transmit and receive data between a server and web application in JSON format. It is also common to store a JSON object in a file. JSON data can be in the form of the object, array, value, string, or number. In Python, JSON exists as a string or more like a dictionary having key-value pairs where keys must be a string and values can be of any type say object, array, value, string, or a number.

Convert JSON String to Python Dictionary using json.load() Function

Python provides json.load() method to convert a JSON file contents to Python Dictionary. Converting JSON files to a dictionary is quite an easy task in python as python script provides a built-in JSON module and JSON has a built-in load() function to carry out the conversion process. Using the same JSON function, we can also convert a JSON string given as input by the user to a dictionary. This method is used when the programmer already has a JSON file with structured data.

Syntax

json.load(file object)

Sample JSON File

This JSON file, we will convert into Python Dictionary.

{
 "Science" : [
   {
	"name" : "Flora"
	"age"  : 18
	"marks": 87
   },
   {
	"name" : "Frank"
	"age"  : 18
	"marks": 76
   }
 ],
 "Commerce" : [
   {
	"name" : "David"
	"age"  : 18
	"marks": 92
   },
   {
	"name" : "Denver"
	"age"  : 19
	"marks": 65
   }
 ]
}

Example

In the following example, we are going to read a JSON file and then print out the data in the form of a dictionary. This json.load() function reads the string from the JSON file. The json.load(file) function creates and returns a new Python dictionary with the key-value pairs in the JSON file. Then, this dictionary is assigned to the data variable, and the result is displayed. You can also check the type of the variable using the built-in type() function of Python.

import json

#opens the JSON file 
with open("sample.json") as json_file:
    data = json.load(json_file)
    
#type of data variable
print("Type:", type(data))
    
#prints the data in dictionary
print("Science Students:", data['Science'])
print("Commerce Students:", data['Commerce'])


Type: <class 'dict'>
Science Students: [{'name': 'Flora', 'age': 18, 'marks': 87}, {'name': 'Frank', 'age': 18, 'marks': 76}]
Commerce Students: [{'name': 'David', 'age': 18, 'marks': 92}, {'name': 'Denver', 'age': 19, 'marks': 65}]

As we have read the JSON file, converted the JSON string into a Python Dictionary, we can now access the data using the index as shown below. This is how we can print the nested data.

#access dictionary using index
print(data["Science"][0])


{'name': 'Flora', 'age': 18, 'marks': 87}

Points to Remember:

1. For reading any JSON file and to work with JSON (string, or file containing JSON object) you must import JSON module in python script.

2. Your JSON file and your Python script must be in the same directory.

3. Your JSON file must follow the JSON standard, so it has to have double quotes rather than single quotes, else it will return JSONDecodeError.

Conclusion:

In the above code, we learn to read a JSON string and convert the data to Python Dictionary. Now, we can access the data using indexes as we do in Python Dictionary.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.