Convert json to dictionary python – In Python, How to Convert JSON to a Dictionary?

Convert json to dictionary python: JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. The text in JSON is represented by a quoted-string, which contains the value in the key-value mapping within flower braces { }.

How to Read from JSON?

Convert json to dict python: In Python, loading a JSON object is simple. Python includes a library named json that may be used to work with JSON data. It is accomplished by utilizing the JSON module, which gives us a plethora of methods, among which the loads() and load() methods will assist us in reading the JSON file.

In order to work with JSON we should first import it into our environment.

import json

Requirements for converting JSON to a dictionary

  • Python’s json module should be imported.
  • If the JSON file is not in the same directory, provide the entire path to it.
  • To avoid the JSONDecodeError, all JSON data (strings) should be contained in double quotes.

Creating a demo JSON File

Here we take student.json as an Example

{
"student_info": [
{
"roll_no": 1,
"name": "john",
"section": "A"
},
{
"roll_no": 2,
"name": "peter",
"section": "B"
},
{
"roll_no": 3,
"name": "alex",
"section": "C"
}
]
}

Convert JSON to a Dictionary in Python

Approach:

  • Import json module using the import keyword.
  • Open the JSON file in read-only mode using the open() method by passing the json file name as an argument to it.
  • Pass the above JSON file as an argument to the json.load() function to convert it into a Python object.
  • Now it becomes a dictionary.
  • Print the type of the above JSON data using the type() function by passing it as an argument to the type() function.
  • Loop in the above JSON data dictionary using the for loop.
  • Print the keys of the above JSON data dictionary.
  • Print the values of the above JSON data dictionary.
  • Close the above-given JSON file using the close() function.
  • The Exit of the Program.

Below is the implementation:

# Import json module using the import keyword
import json
# Open the json file in read-only mode using the open() method by passing the json file name 
# as an argument to it
json_file = open("demo.json", 'r')
 
# Pass the above json file as an argument to the json.load() function to convert it into Python object
# Now it becomes a dictionary
jsondata = json.load(json_file)
 
# Print the type of the above json data using the type() function by passing it 
# as an argument to the type() function 
print("Type:", type(jsondata))
 
# Loop in the above json data dictionary using the for loop
for dict_key, dict_value in jsondata.items():
    # Print the keys of the above json data dictionary
    print(f"\nKey: {dict_key}")
    # Print the values of the above json data dictionary
    print(f"Value: {dict_value}\n")
 
# Close the above given Json file using the close()function
json_file.close()

Output:

Type: <class 'dict'>

Key: student_info
Value: [{'roll_no': 1, 'name': 'john', 'section': 'A'}, {'roll_no': 2, 'name': 'peter', 'section': 'B'},
 {'roll_no': 3, 'name': 'alex', 'section': 'C'}]