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())

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

Read more
Suggested changes
-
    content = json.loads(f.read())
+
    content = json.load(f)
Commit suggestion
Update catalogue.py

Instantly check if you have this issue for free

    Works with tools you use

    Read about how it works.