Last modified: May 28, 2026
Python Json Loads From File
Working with JSON data is a common task in Python. You often need to read JSON from a file. The correct method for this is json.load(), not json.loads(). Many beginners confuse these two. This article explains the difference and shows you how to load JSON from a file correctly.
Understanding json.load() vs json.loads()
json.load() reads JSON from a file object. json.loads() reads JSON from a string. The "s" in loads stands for string. Use json.load() when you have a file. Use json.loads() when you have a string.
Both methods return a Python dictionary. This dictionary is easy to work with. You can access keys and values directly.
How to Use json.load() on a File
First, import the json module. Then open the file in read mode. Use the open() function with the 'r' flag. Pass the file object to json.load().
import json
# Open the file in read mode
with open('data.json', 'r') as file:
# Load JSON data from file
data = json.load(file)
# Now 'data' is a Python dictionary
print(data)
This code opens data.json. It reads the content and converts it to a dictionary. The with statement closes the file automatically. This is the safest way.
Example with a Real JSON File
Create a file named users.json with this content:
{
"users": [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35}
]
}
Now load this file in Python:
import json
with open('users.json', 'r') as file:
data = json.load(file)
# Access the list of users
users = data['users']
# Loop through each user
for user in users:
print(f"{user['name']} is {user['age']} years old.")
The output will be:
Alice is 30 years old.
Bob is 25 years old.
Charlie is 35 years old.
Notice: The file must be valid JSON. If the file has syntax errors, Python will raise a json.JSONDecodeError.
Handling File Not Found Errors
Always handle missing files. Use a try-except block to catch FileNotFoundError. This makes your code robust.
import json
try:
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
except FileNotFoundError:
print("The file was not found.")
except json.JSONDecodeError:
print("The file is not valid JSON.")
This code catches two common errors. It prints a friendly message instead of crashing.
Common Mistakes Beginners Make
Mistake 1: Using json.loads() on a file object. This will fail because json.loads() expects a string, not a file.
# Wrong way
import json
with open('data.json', 'r') as file:
data = json.loads(file) # This will raise TypeError
Mistake 2: Opening the file in write mode. Always use 'r' for reading.
Mistake 3: Forgetting to close the file. Use the with statement to avoid this.
When to Use json.loads() Instead
Use json.loads() when you have a JSON string. For example, from an API response or a user input.
import json
json_string = '{"name": "Alice", "age": 30}'
data = json.loads(json_string)
print(data['name']) # Output: Alice
If you have a file, always use json.load(). If you have a string, use json.loads(). Remember the "s" stands for string.
Working with Large JSON Files
For large files, json.load() reads everything into memory. This can be slow. Use json.JSONDecoder for streaming. Or use ijson library for iterative parsing.
For most cases, json.load() works fine. Only optimize if you have files over 100 MB.
Conclusion
Loading JSON from a file in Python is simple. Use json.load() with a file object. Always handle errors with try-except. Remember the difference between json.load() and json.loads(). Practice with small files first. This skill is essential for working with APIs, configuration files, and data storage.
Now you can confidently read JSON data from files in Python. Start using this in your projects today.