How to Solve Python TypeError: the JSON object must be str, bytes or bytearray, not ‘TextIOWrapper’

by | Programming, Python, Tips

This error occurs when you pass a File object to the json.loads() file. The json.loads() method expects a string, bytes or a bytearray. You can solve this error by calling the read() method on the file object to get a string or passing the file object to the json.load() method.

This tutorial will go through how to solve the error with code examples.


TypeError: the JSON object must be str, bytes or bytearray, not ‘TextIOWrapper’

TypeError occurs in Python when you perform an illegal operation for a specific data type. TextIOWrapper is the file object returned when the open() function opens a file. The _io.TextIOWrapper class provides methods and attributes to help us read and write data to and from the file. The json.loads() method expects an object of type str, bytes or bytearray.

Example

Consider the following example data stored in a file called particles.json. The data contains the names of fundamental particles and their masses in MeV as a list of dictionaries.

[
        {"proton":938.3},
        {"neutron":939.6},
        {"electron":0.51}
]

Let’s try to read the data into our program using a context manager and print the data to the console.

with open('particles.json', 'r') as f:
    data = json.loads(f)

print(data)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [14], in <cell line: 1>()
      1 with open('particles.json', 'r') as f:
----> 2     data = json.loads(f)
      4 print(data)

File ~/opt/anaconda3/lib/python3.8/json/__init__.py:341, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    339 else:
    340     if not isinstance(s, (bytes, bytearray)):
--> 341         raise TypeError(f'the JSON object must be str, bytes or bytearray, '
    342                         f'not {s.__class__.__name__}')
    343     s = s.decode(detect_encoding(s), 'surrogatepass')
    345 if "encoding" in kw:

TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper

The error occurs because the json.loads() expects a JSON string, but we gave it a File object. We use the json.loads() method for deserializing native string, byte, or byte array consisting of JSON data into a Python object.

Solution #1: Call the read() method

We can solve this error by calling the read() method on the File object, which returns a string containing the JSON data. Let’s look at the revised code:

with open('particles.json', 'r') as f:

    # Call read method to get JSON string

    string = f.read()

    print(string)

    print(type(string))

    # Pass JSON string to loads() method

    data = json.loads(string)
    
# Print result

print(data)

print(type(data))

We split the process into smaller steps to see how we create each object. Let’s run the code to see the result:

[
        {"proton":938.3},
        {"neutron":939.6},
        {"electron":0.51}
]

<class 'str'>
[{'proton': 938.3}, {'neutron': 939.6}, {'electron': 0.51}]
<class 'list'>

The first object with the variable name string is a string representing the contents of the JSON file. We then pass this object to the json.loads() method, which returns a Python object, specifically a list containing the key-value pairs for the three particles.

Solution #2: Use json.load()

We can also solve the error by passing the File object to the json.load() method. With json.load() we can read JSON data from text, JSON, or binary files. The json.load() method returns a Python object.

Let’s look at the revised code:

with open('particles.json', 'r') as f:
    
    data = json.load(f)
    

print(data)
print(type(data))

Let’s run the code to see the result:

[{'proton': 938.3}, {'neutron': 939.6}, {'electron': 0.51}]
<class 'list'>

We successfully retrieved a list of dictionaries using the json.load() method.

Summary

Congratulations on reading to the end of this tutorial.

For further reading on errors involving JSON, go to the articles:

For further reading on errors involving TextIOWrapper, go to the article:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee