The JSON object must be str or bytes not TextIOWrapper

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

The JSON object must be str or bytes not TextIOWrapper #

The Python "TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper" occurs when we pass a file object to the json.loads() method. To solve the error, pass the file object to the json.load() method instead.

typeerror the json object must be str bytes or bytearray not tuple

Here is an example of how the error occurs.

main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: # ⛔️ TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper my_data = json.loads(f)

We can't pass a file object directly to the json.loads() method, but we can use the json.load() method to deserialize a file to a Python object.

main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: my_data = json.load(f) print(my_data) # 👉️ {'name': 'Alice', 'age': 30} print(type(my_data)) # 👉️ <class 'dict'>

The code sample above assumes that you have an example.json file in the same directory.

example.json
{"name": "Alice", "age": 30}

The json.load method is used to deserialize a file to a Python object, whereas the json.loads method is used to deserialize a JSON string to a Python object.

The json.load() method expects a text file or a binary file containing a JSON document that implements a .read() method.

Alternatively, you manually call the read() method on the file object and use the json.loads() method.

main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: # 👇️ make sure to call read() my_data = json.loads(f.read()) print(my_data) # 👉️ {'name': 'Alice', 'age': 30} print(type(my_data)) # 👉️ <class 'dict'>

The example above achieves the same result, but instead of relying on the json.load() method to call read() on the file object for us, we manually do it and use the json.loads() method instead.

If you need to parse a JSON string into a native Python object, you have to use the json.loads() method, and if you need to convert a Python object into a JSON string, you have to use the json.dumps() method.

main.py
import json json_str = r'{"name": "Alice", "age": 30}' # ✅ parse JSON string to Python native dict my_dict = json.loads(json_str) print(type(my_dict)) # 👉️ <class 'dict'> # ✅ convert Python native dict to a JSON string my_json_str = json.dumps(my_dict) print(type(my_json_str)) # 👉️ <class 'str'>

The json.loads() method basically helps us load a Python native object (e.g. a dictionary or a list) from a JSON string.

The JSONEncoder class supports the following objects and types by default.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

If you aren't sure what type of object a variable stores, use the built-in type() class.

main.py
my_dict = {'name': 'Alice', 'age': 30} print(type(my_dict)) # 👉️ <class 'dict'> print(isinstance(my_dict, dict)) # 👉️ True my_str = 'hello world' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Conclusion #

The Python "TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper" occurs when we pass a file object to the json.loads() method. To solve the error, pass the file object to the json.load() method instead.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.