json
Python
json.load
json.loads
programming

What is the difference between json.load and json.loads functions

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

json.load and json.loads both parse JSON into Python objects, but they do not take the same kind of input. The difference is short and important: json.load reads from a file-like object, while json.loads reads from a string, bytes, or bytearray.

json.load Reads From an Open File

Use json.load when you already have a file object:

python
1import json
2
3with open("config.json", "r", encoding="utf-8") as f:
4    data = json.load(f)
5
6print(data)

Here, json.load reads the file contents for you and parses them into Python data structures such as dictionaries, lists, strings, and numbers.

This is the right choice when the JSON source lives on disk or comes from another file-like stream.

json.loads Parses a JSON String

Use json.loads when the JSON is already in memory as text:

python
1import json
2
3text = '{"name": "Ada", "active": true, "score": 9}'
4data = json.loads(text)
5
6print(data["name"])

Despite the similar name, loads does not mean "load from file." The trailing s stands for "string."

It is also valid for bytes and bytearray:

python
payload = b'{"status": "ok"}'
print(json.loads(payload))

A Useful Way to Remember It

Think of the naming pairs:

  • 'json.load reads JSON from a stream'
  • 'json.loads reads JSON from a string'
  • 'json.dump writes JSON to a stream'
  • 'json.dumps writes JSON to a string'

The s forms work with strings. The versions without s work with file-like objects.

Practical Example: HTTP Versus File

If you fetch data from an HTTP client and already have the response text, use loads:

python
1import json
2
3response_text = '{"items": [1, 2, 3]}'
4data = json.loads(response_text)
5print(data["items"])

If you save the response to disk and later reopen the file, use load:

python
1import json
2
3with open("response.json", "w", encoding="utf-8") as f:
4    f.write('{"items": [1, 2, 3]}')
5
6with open("response.json", "r", encoding="utf-8") as f:
7    data = json.load(f)
8
9print(data["items"])

Both produce the same Python object. The only difference is where the JSON input starts from.

Common Errors

One common mistake is passing a filename string to json.load:

python
json.load("config.json")

That fails because json.load expects a file object, not a path string. The correct form is:

python
with open("config.json", "r", encoding="utf-8") as f:
    data = json.load(f)

Another mistake is passing an already opened file object to json.loads. That also fails because loads expects textual content, not a file handle.

Performance and Behavior Notes

For normal applications, choose based on input type, not performance. Internally, the main practical difference is still file object versus in-memory string.

If you already have the text in memory, loads avoids an unnecessary extra file step. If you are reading from disk, load keeps the code direct and readable.

In both cases, the parsed result is the same kind of Python object. The distinction is about where the JSON comes from, not about a special output format.

Common Pitfalls

The biggest mistake is thinking the s in loads means plural. It means string.

Another issue is mixing up file paths and file objects. A path like "config.json" is not the same thing as an opened handle returned by open(...).

A third problem is confusing parsing functions with serialization functions. load and loads parse JSON into Python objects, while dump and dumps go the other direction.

Summary

  • 'json.load(f) parses JSON from an open file-like object.'
  • 'json.loads(text) parses JSON from a string, bytes, or bytearray.'
  • The trailing s means the function works with strings.
  • Use dump and dumps for the reverse direction, from Python objects to JSON.
  • Choose the function based on the kind of input you already have.

Course illustration
Course illustration

All Rights Reserved.