Skip to main content

How to Read JSON from a File in Python with json.load

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings, saved results and the answers of web APIs are often JSON. json.load reads it from an open file, or anything that behaves like one, and gives you ordinary Python values: a dictionary for each JSON object and a list for each array. This guide reads a small JSON file on Python 3.15.2.

Syntax​

Syntax
import json

with open("<file>.json", encoding="utf-8") as file:
<data> = json.load(file)
PartWhat it means
open("<file>.json", encoding="utf-8")Opens the file for reading as text. JSON files are UTF-8
json.load(file)Reads the whole file and turns the JSON into Python values
<data>The result: a dict for a JSON object, a list for an array

Python's json documentation describes its argument as "A .read()-supporting text file or binary file containing the JSON document to be deserialized."

The Example​

A file named book.json describes a book. The script reads it, prints the result, uses two of its values, and shows that the JSON array came back as a Python list.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Create the JSON File​

book.json
{"title": "The Hobbit", "year": 1937, "tags": ["fantasy", "adventure"]}

Step 2: Write the Script​

json_load_demo.py
import json

with open("book.json", encoding="utf-8") as file:
book = json.load(file)

print(book)
print(book["title"], book["year"])
print(type(book["tags"]).__name__)

The json_load_demo.py script in a macOS-style code window: it opens book.json with UTF-8 encoding, loads it with json.load into book, prints book, prints the title and year, and prints the type name of book's tags.

Step 3: Run It​

Terminal
uv run python json_load_demo.py
Output
{'title': 'The Hobbit', 'year': 1937, 'tags': ['fantasy', 'adventure']}
The Hobbit 1937
list

The terminal after uv run python json_load_demo.py: the dictionary with title The Hobbit, year 1937 and two tags, then The Hobbit 1937, then list.

The JSON object became a dictionary, so book["title"] reads the title. The number stayed a number (1937, without quotes), and the array became a list. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Passing JSON text to json.load: json.load('{"title": "The Hobbit"}')AttributeError: 'str' object has no attribute 'read', because load calls .read() on what you give itUse json.loads for text
Leaving out encoding="utf-8"Fine on Python 3.15, where "Python now uses UTF-8 as the default encoding" (What's New in 3.15). Older versions use the system's locale encoding, which can garble accented letters on WindowsPass encoding="utf-8", so the code reads the file the same way on every version

Troubleshooting​

SymptomLikely causeFix
AttributeError: 'str' object has no attribute 'read'The argument is text, not a filejson.loads(text)
KeyError: 'title'The JSON has no such key, or it's spelled differentlyPrint the dictionary first and check the key

FAQ​

What's the difference between json.load and json.loads?​

load reads from a file. loads reads from text: the documentation calls it "Identical to load(), but instead of a file-like object", deserializing a string. json.loads('{"title": "The Hobbit"}') returns {'title': 'The Hobbit'}.

Can json.load read a web response?​

Yes, if the response has a .read() method, as the object urllib.request.urlopen returns does: json.load(response).

Conclusion​

json.load(file) turned book.json into a dictionary whose title, year and list of tags were ready to use. Use json.load for files and file-like objects, and json.loads for text.

Additional Resources​