Use json.load to read a JSON file

json.load(f) simplifies reading JSON from disk.

If you ever wondered why json.loads ends with a "s" it's because it's specifically for working on a string, but there is another related method: json.load

json.loads is for deserialising a string containing JSON, while json.load (no "s") is for deserialising a file containing JSON.

The methods are very related. In fact, json.load is a wrapper around json.loads. It's a method provided out of the box by Python to simplify the task of reading JSON from file-like objects.

Python core developers are very careful about what features are included in the Python builtin modules, so it makes sense to utilise the convenience methods and not reinvent the wheel? For example, these result in the same outcome:

So why not choose the easiest to read, write, and execute (the first one)?

If our GitHub code review bot spots this issue in your pull request it gives this advice:

code-review-doctorbotsuggested changes just now
catalogue.py
1
+
with open('some/path.json') as f:
2
+
    content = json.loads(f.read())
Suggested changes
-
    content = json.loads(f.read())
+
    content = json.load(f)
Commit suggestion

json.load(f) simplifies reading JSON from disk.

Read more
Update catalogue.py
We're your Python code review copilot. Get code improvements right in your pull request with our GitHub code review bot.

Configuring this check

Code Review Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code use-json-load in your pyproject.toml file.

Read more about configuring Code Review Doctor.