Read list of dictionaries from JSON file in Python : 4 Steps

If you have stored list of dictionaries in  JSON format in a file then how you can read it in python. In this entire tutorial you will learn how to read list list of dictionaries from json file with steps.

Sample JSON File

The following is the content of the JSON File. It contained a list of dictionaries.

data.json

[
  {
    "country": "USD",
    "capital": "Washington DC",
    "dial_code": 1
  },
  {
    "name": "UK",
    "capital": "London",
    "dial_code": 44
  }
]

Steps to read the list of dictionaries from JSON file

Step 1: Import the JSON module

This module comes with python. You can import it using the import statement.

import json

Step 2: Open the data.json file

The second step is to open the JSON file named data.json. Use the open() function for it.

with open('data.json', encoding='utf-8') as json_file:

Step 3:  Load the data

Use the function json.load(json_file) to load all the data inside the JSON file to the data variable.

data = json.load(f)

Step 4: Print the data

After storing the  JSON data inside the data variable print out the data.

print(data)

That’s all you have to follow to read the list of dictionaries from the JSON file.

Full Code

import json
with open("data.json") as json_file :
  data = json.load(json_file)
print(data)
print(type(data))

Output

[{'country': 'USD', 'capital': 'Washington DC', 'dial_code': 1}, {'name': 'UK', 'capital': 'London', 'dial_code': 44}]

Summary

You can easily read the list of dictionaries from the JSON File in python using the json.load() function. In this tutorial, you have learned how to read the JSON file and display it as a list of dictionaries using simple steps.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link