close

[Solved] TypeError: Object of type ‘bytes’ is not JSON serializable

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: Object of type ‘bytes’ is not JSON serializable in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: Object of type ‘bytes’ is not JSON serializable Error Occurs?

Today I get the following error TypeError: Object of type ‘bytes’ is not JSON serializable in python.

How To Solve TypeError: Object of type ‘bytes’ is not JSON serializable Error ?

  1. How To Solve TypeError: Object of type 'bytes' is not JSON serializable Error ?

    To Solve TypeError: Object of type 'bytes' is not JSON serializable Error Each of those t.encode()l.encode() and d.encode() calls creates a bytes string. Do not do this, leave it to the JSON format to serialise these.

  2. TypeError: Object of type 'bytes' is not JSON serializable

    To Solve TypeError: Object of type 'bytes' is not JSON serializable Error Each of those t.encode()l.encode() and d.encode() calls creates a bytes string. Do not do this, leave it to the JSON format to serialise these.

Solution 1

You are creating those bytes objects yourself:

item['title'] = [t.encode('utf-8') for t in title]
item['link'] = [l.encode('utf-8') for l in link]
item['desc'] = [d.encode('utf-8') for d in desc]
items.append(item)

Each of those t.encode()l.encode() and d.encode() calls creates a bytes string. Do not do this, leave it to the JSON format to serialise these.

Next, you are making several other errors; you are encoding too much where there is no need to. Leave it to the json module and the standard file object returned by the open() call to handle encoding.

You also don’t need to convert your items list to a dictionary; it’ll already be an object that can be JSON encoded directly:

class W3SchoolPipeline(object):    
    def __init__(self):
        self.file = open('w3school_data_utf8.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        line = json.dumps(item) + '\n'
        self.file.write(line)
        return item

I’m guessing you followed a tutorial that assumed Python 2, you are using Python 3 instead. I strongly suggest you find a different tutorial; not only is it written for an outdated version of Python, if it is advocating line.decode('unicode_escape') it is teaching some extremely bad habits that’ll lead to hard-to-track bugs. I can recommend you look at Think Python, 2nd edition for a good, free, book on learning Python 3.

Solution 2

Simply write <variable name>.decode("utf-8").

For example:

myvar = b'asdqweasdasd'
myvar.decode("utf-8")

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read