Solving Python TypeError: the JSON object must be str, not ‘bytes’

Python TypeError: the JSON object must be str, not ‘bytes’ is an error which happens when you do not decode your data correctly.

This post is a guide showing you why you are having this error and how you can get rid of it in the most efficient way possible, I will also include some alternative solutions that may help you.

Explaining Python TypeError: the JSON object must be str, not ‘bytes’

This is an error which happens when you do not decode your data correctly.

The error message should look like the error in the example bellow, make sure you have the same error message in order to avoid confusion.

                                                                       #
TypeError: the JSON object must be str, not 'bytes'
                                                                       #

To solve the problem above, I have 2 solutions which worked for me, bellow is a detailed explanation of both.

Solution 1 : use requests. requests handles the decoding automatically.

The first solution is to use requests, this is very easy and convenient since requests handles the decoding automatically.

Request has a built in json function which you can use like in the example bellow.

                                                                       #
data = response.json()
                                                                       #

This is sometimes better than doing the decoding manually since it automatically detects how to correctly decode the content.

Try the solution bellow. If this solution was not enough.

Solution 2 : upgrade python.

The second solution is very simple. Just upgrade python. By only the newest versions of python you will completely avoid this error.

This error is only for those who do not want to stick to one python version/installation.

Be careful with this, since upgrading to a new python version can sometimes break other things in your code and cause more errors.

If you do not want to upgrade python or this solution did not solve your problem, try the solution bellow.

Solution 3 : use chardet and detect.

The last solution is to try the chardet library, the library contains a function called detect, detect can automatically encode your string using most common encodings.

You can use chardet like in the example bellow.

                                                                       #
import chardet
json.loads(myResponse.content.decode(chardet.detect(myResponse.content)["encoding"]))
                                                                       #

I hope the fix above fixed your problem, good luck with the scripts to come.

Summing-up

The end of this article is here, please make sure to support the team by clicking the Kofi button, it is the red button on the top of this page, it is for donations and will help us help more developers in our error solving journey. Please keep learning and keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/