The simplest way to read a JSON file in Python
To read a JSON file in Python, use the json module that comes built into Python, along with Python's standard file-opening function. Open the file, pass it to json.load(), and Python converts the JSON data into a Python dictionary or list you can work with immediately.
Here is the actual code:
import json with open('data.json', 'r') as file: data = json.load(file) print(data)Replace 'data.json' with the name of your file. The with statement opens the file and closes it automatically when you are done, which prevents errors. The 'r' means read-only mode. After this runs, data holds your JSON content as a Python object — usually a dictionary with key-value pairs, or a list of items.
Key Takeaways
- The json.load() function reads a JSON file directly from disk and converts it to a Python dictionary or list in one step.
- Always use the with statement when opening files so Python closes them automatically and prevents data corruption.
- If your JSON is stored as a string in memory instead of a file, use json.loads() (with an 's') instead of json.load().
- JSON objects become Python dictionaries, and JSON arrays become Python lists, so you access values the same way you would in any Python data structure.
Understanding the difference between json.load() and json.loads()
json.load() reads from a file on your computer. json.loads() reads from a string that already exists in your Python code. The 's' stands for 'string'.
Use json.load() when you have a .json file. Use json.loads() when someone sends you JSON as text, or when you receive it from a web request. Here is an example of json.loads():
import json json_string = '{"name": "Alice", "age": 30}' data = json.loads(json_string) print(data['name']) # Output: AliceBoth functions do the same conversion — they turn JSON into Python objects. The only difference is where the JSON comes from.
Accessing data after you read the JSON file
Once you have read the JSON file, you access the data like any other Python dictionary or list. If your JSON file contains an object (curly braces), it becomes a dictionary. If it contains an array (square brackets), it becomes a list.
For a JSON file that looks like this:
{ "name": "Bob", "age": 25, "city": "Portland" }You would read it and access values like this:
import json with open('person.json', 'r') as file: data = json.load(file) print(data['name']) # Output: Bob print(data['age']) # Output: 25If your JSON file is an array of objects, loop through it the same way you would loop through any Python list:
import json with open('people.json', 'r') as file: people = json.load(file) for person in people: print(person['name'])Handling errors when the JSON file is broken or missing
If the JSON file does not exist, Python raises a FileNotFoundError. If the file exists but contains invalid JSON, Python raises a json.JSONDecodeError. You can catch these errors and handle them gracefully instead of letting your program crash.
Here is how to do it:
import json try: with open('data.json', 'r') as file: data = json.load(file) except FileNotFoundError: print("The file was not found.") except json.JSONDecodeError: print("The file contains invalid JSON.")The try block contains the code that might fail. The except blocks run only if that specific error occurs. This way, your program can tell the user what went wrong instead of stopping without explanation.
Reading large JSON files without loading everything into memory
If your JSON file is very large, loading all of it at once with json.load() uses a lot of memory. For files that contain an array of many objects, you can read one object at a time using the ijson library, which is not built in but can be installed with pip install ijson.
However, for most everyday tasks, json.load() is fine. Only use ijson if you are working with files larger than your available memory, or if you are processing millions of records. For typical work — reading configuration files, API responses, or data exports — the standard json.load() is simpler and fast enough.
If you do need ijson, the syntax looks like this:
import ijson with open('large_file.json', 'r') as file: for obj in ijson.items(file, 'item'): print(obj)This reads one object at a time instead of the entire file.
Common mistakes when reading JSON files
The most common mistake is forgetting to import the json module at the top of your script. If you write json.load() without import json first, Python will tell you that json is not defined.
Another mistake is using the wrong file path. If your JSON file is in a different folder, you need to include the path. For example, if the file is in a subfolder called data, write open('data/file.json', 'r') instead of just open('file.json', 'r'). On Windows, you can use forward slashes or backslashes — Python handles both.
A third mistake is confusing json.load() and json.dump(). Use json.load() to read a file. Use json.dump() to write Python data to a JSON file. The names are easy to mix up, but they do opposite things.
Frequently Asked Questions
What if my JSON file has a different encoding, like UTF-8 with a BOM?
Python 3 assumes UTF-8 by default, which works for most JSON files. If you get a decoding error, add the encoding parameter: open('file.json', 'r', encoding='utf-8-sig'). The -sig part tells Python to ignore the byte order mark that some editors add to the start of the file.
Can I read JSON directly from a website URL?
Yes, but you need the requests library, which is not built in. Install it with pip install requests, then use response = requests.get(url) and data = response.json(). This downloads the JSON and converts it in one step, without saving it to disk first.
What is the difference between JSON and Python dictionaries?
JSON is a text format for storing data. Python dictionaries are Python objects that live in memory. When you read JSON with json.load(), Python converts the JSON text into a dictionary. They look similar because JSON was designed to match JavaScript objects, and Python dictionaries are similar to JavaScript objects.
Do I need to close the file after reading it with json.load()?
No, the with statement closes it automatically. If you open a file without with, you must call file.close() yourself, but using with is the standard way and prevents mistakes.