The simplest way to read a JSON file

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 when ready.

Here is the actual code:

import json with open('filename.json') as file:     data = json.load(file) print(data)

That is all you need for most cases. The with open() statement opens the file and closes it automatically when you are done, which prevents errors. The json.load() function reads the file and converts it into a Python object — usually a dictionary (similar to a spreadsheet with labeled columns) or a list (similar to a numbered row).

Key Takeaways

  • Use json.load() to read a file that already exists on your computer, and json.loads() to read JSON text that is already in your Python code.
  • Always use with open() instead of plain open(), because it closes the file automatically and prevents data loss.
  • JSON converts into Python dictionaries and lists, so you access the data using bracket notation like data['key'] or data[0].
  • If the JSON file is malformed or uses the wrong encoding, Python will raise an error that tells you exactly which line is broken.

Understanding what json.load() actually does

The json module is Python's translator between JSON format (text that looks like {"name": "James", "age": 30}) and Python objects (dictionaries and lists that your code can actually use). When you call json.load(), it reads the entire file, checks that the JSON is valid, and converts it into a Python structure.

A JSON object like {"name": "James", "age": 30} becomes a Python dictionary, which you access with keys: data['name'] returns "James". A JSON array like [1, 2, 3] becomes a Python list, which you access with index numbers: data[0] returns 1. If your JSON file contains an array of objects — like a list of people, each with a name and age — you get a list of dictionaries, and you loop through it with for person in data:.

Reading JSON from text instead of a file

If you have JSON text already in your Python code (not in a separate file), use json.loads() instead of json.load(). The difference is one letter: load() reads from a file, loads() reads from a string.

import json json_text = '{"name": "James", "age": 30}' data = json.loads(json_text) print(data['name'])

This is useful when you receive JSON from a website API or from another program, and you need to convert it into something Python can work with. The rest of the code works exactly the same — you get a dictionary or list and access it the same way.

Handling errors when the JSON is broken

If your JSON file has a syntax error — a missing comma, a quote in the wrong place, or unmatched brackets — Python raises a json.JSONDecodeError and tells you which line is wrong. You can catch this error and handle it gracefully instead of letting your program crash.

import json try:     with open('filename.json') as file:         data = json.load(file) except json.JSONDecodeError as error:     print(f"JSON error on line {error.lineno}: {error.msg}")

The try and except block tells Python: "Try to read the file, but if something goes wrong, print a helpful message instead of crashing." The error object gives you the line number and a description of what went wrong, which makes it much faster to find the problem in your JSON file.

Working with nested JSON structures

Real JSON files often have data nested inside other data — for example, a list of people where each person has an address object, and that address has a street, city, and zip code. Python handles this naturally because dictionaries can contain other dictionaries and lists.

import json with open('people.json') as file:     data = json.load(file) for person in data:     print(person['name'])     print(person['address']['city'])

If your JSON file contains an array of people, the for person in data: loop goes through each one. Inside the loop, person['name'] gets the name, and person['address']['city'] drills down into the nested address object to get the city. You can nest as many levels deep as your JSON goes — just keep adding brackets for each level.

Specifying the file encoding

Most JSON files use UTF-8 encoding, which Python assumes by default. If your JSON file uses a different encoding (rare, but it happens), or if you get an error about encoding when you try to open the file, specify the encoding explicitly in the open() function.

import json with open('filename.json', encoding='utf-8') as file:     data = json.load(file)

The encoding='utf-8' parameter tells Python exactly how to interpret the bytes in the file. If you receive a file from someone else and get an encoding error, ask them what encoding they used, or try encoding='latin-1' or encoding='cp1252' if the file came from Windows. Most of the time, UTF-8 works without you having to specify it.

Reading large JSON files efficiently

If your JSON file is very large (hundreds of megabytes or more), loading the entire file into memory at once can slow down your computer. For files that contain an array of objects, you can read and process one object at a time using a library like ijson, which streams the data instead of loading it all at once.

For most everyday use — configuration files, API responses, data exports under a few megabytes — the standard json.load() approach is fast enough and simpler to understand. Only optimize for streaming if you actually run into memory problems. You can install ijson with pip install ijson if you need it, but start with the basic approach first.

Frequently Asked Questions

What is the difference between json.load() and json.loads()?

json.load() reads from a file object (something you opened with open()), while json.loads() reads from a string of text. Use load() when your JSON is in a file on disk, and loads() when the JSON is already in your Python code or came from a website response.

Why does Python say "JSONDecodeError" when I try to read my file?

Your JSON file has a syntax error — usually a missing comma, a mismatched bracket, or a quote in the wrong place. The error message tells you which line is broken. Open the file in a text editor, go to that line, and look for the mistake. A JSON validator website can also check your file for errors.

How do I access data inside nested objects?

Use bracket notation for each level: data['person']['address']['city'] drills down through nested dictionaries. If you have a list of objects, loop through the list first with for item in data:, then access the nested keys inside the loop.

Can I modify the JSON data and save it back to the file?

Yes. After you read the JSON with json.load(), modify the Python dictionary or list however you want. Then use json.dump() to write it back: with open('filename.json', 'w') as file: json.dump(data, file). The 'w' mode opens the file for writing.