The basic way to read a JSON file
To read a JSON file in Python, use the json module that comes built into Python. The simplest approach is to open the file and use json.load() to convert the JSON data into a Python dictionary or list that you can work with.
Here is the most common pattern: import the json module, open your file, and load it. If your JSON file is called data.json and sits in the same folder as your Python script, this code reads it:
import json with open('data.json') as file: data = json.load(file) print(data)
The with statement opens the file and closes it automatically when you are done. The json.load() function reads the file and converts it into a Python object — usually a dictionary if your JSON starts with curly braces, or a list if it starts with square brackets.
Key Takeaways
- Use json.load() to read a JSON file into Python; it converts JSON data into a dictionary or list you can use immediately.
- Always use the with statement when opening files so Python closes them automatically and prevents errors.
- If the file is in a different folder, provide the full path: open('/path/to/data.json') on Mac or Linux, or open('C:\\path\\to\\data.json') on Windows.
- Use json.loads() (with an 's') if you have JSON as a string instead of a file, or if you need to read JSON from a web response.
- Handle errors with a try-except block to catch problems like missing files or invalid JSON format.
Accessing data after you load it
Once you load the JSON file, Python gives you a dictionary or list. Access the data the same way you would any other Python object. If your JSON file contains a dictionary with keys, use bracket notation or dot notation to get values.
For example, if data.json looks like this:
{ "name": "Alice", "age": 30, "city": "Portland" }
You can read the file and access the values like this:
import json with open('data.json') as file: data = json.load(file) print(data['name']) # Prints: Alice print(data['age']) # Prints: 30
If your JSON file contains a list of objects, loop through it to access each item:
import json with open('people.json') as file: people = json.load(file) for person in people: print(person['name'])
Handling file paths and missing files
If your JSON file is not in the same folder as your Python script, you need to provide the correct path. On Mac or Linux, paths use forward slashes: '/home/user/documents/data.json'. On Windows, use backslashes or raw strings: 'C:\\Users\\user\\Documents\\data.json' or r'C:\Users\user\Documents\data.json'.
If the file does not exist or the path is wrong, Python raises a FileNotFoundError. Wrap your code in a try-except block to handle this gracefully:
import json try: with open('data.json') as file: data = json.load(file) except FileNotFoundError: print("File not found. Check the path and try again.") except json.JSONDecodeError: print("Invalid JSON format in the file.")
The JSONDecodeError catches problems where the file exists but contains invalid JSON — for example, missing commas or quotes. This approach prevents your script from crashing.
Reading JSON from a string instead of a file
Sometimes you have JSON as a string rather than a file — for instance, when you receive data from a web API or a database. Use json.loads() (note the 's' at the end) to convert a JSON string into a Python object.
Here is the difference: json.load() reads from a file, and json.loads() reads from a string:
import json json_string = '{"name": "Bob", "age": 25}' data = json.loads(json_string) print(data['name']) # Prints: Bob
This is useful when you fetch data from an API using a library like requests. The response body is a string, so you use json.loads() to parse it. Many libraries like requests also have built-in methods (such as response.json()) that do this automatically.
Working with large JSON files
If your JSON file is very large, loading the entire file into memory at once can slow down your program. For most files under a few megabytes, json.load() works fine. For larger files, consider reading the file line by line or using a streaming JSON parser.
If your large file contains one JSON object per line (called JSONL or newline-delimited JSON), read it line by line:
import json with open('large_data.jsonl') as file: for line in file: data = json.loads(line) print(data)
This approach processes one object at a time instead of loading everything into memory. For standard JSON files that are not line-delimited, you may need a third-party library like ijson if the file is extremely large.
Converting Python objects back to JSON
After you read and modify data, you may want to save it back to a JSON file. Use json.dump() to write a Python dictionary or list to a file as JSON:
import json data = {'name': 'Charlie', 'age': 35} with open('output.json', 'w') as file: json.dump(data, file)
The 'w' mode opens the file for writing. If the file does not exist, Python creates it. If it does exist, Python overwrites it. To make the JSON more readable with indentation, add the indent parameter:
json.dump(data, file, indent=2)
This adds two spaces of indentation to each level, making the output easier to read in a text editor.
Common mistakes and how to fix them
One frequent error is forgetting to import the json module. Always start with import json at the top of your script. Another mistake is using the wrong function: remember that json.load() reads from a file, and json.loads() reads from a string.
If you get a JSONDecodeError, the file contains invalid JSON. Check for missing commas between key-value pairs, unmatched quotes, or trailing commas (which JSON does not allow). Use a JSON validator tool online to find the exact problem.
If you get a KeyError when accessing data, the key you are looking for does not exist in the dictionary. Use the get() method to provide a default value instead of crashing:
name = data.get('name', 'Unknown') # Returns 'Unknown' if 'name' key is missing
Frequently Asked Questions
What is the difference between json.load() and json.loads()?
json.load() reads JSON from a file object that you open with the open() function. json.loads() reads JSON from a string. Use load() when you have a file, and loads() when you have a string — the 's' stands for 'string'.
Can I read a JSON file that is on the internet?
Yes, but you need to fetch it first using a library like requests. Call requests.get(url) to download the file, then use response.json() to parse it directly, or use json.loads(response.text) to convert the response text to a Python object.
What should I do if the JSON file is corrupted or has invalid syntax?
Python will raise a json.JSONDecodeError when it tries to read invalid JSON. Use a try-except block to catch this error and print a message. To find the problem, open the file in a text editor or use an online JSON validator to check the syntax.
How do I read only part of a large JSON file?
If the file contains one JSON object per line, read it line by line and stop when you have what you need. For a standard JSON file, you must load the entire structure into memory first, then filter the data using Python loops and conditionals.
Can I modify the data and save it back to the same file?
Yes. Read the file with json.load(), modify the Python dictionary or list, then write it back with json.dump() using 'w' mode. Be careful — this overwrites the original file, so make a backup first if the data is important.