How To Solve “The JSON object must be str, bytes or bytearray, not list” Error

The JSON object must be str, bytes or bytearray, not list

Are you finding a way to fix the error “The JSON object must be str, bytes or bytearray, not list”? Let’s follow this article. We will help you to fix it.

The cause of “The JSON object must be str, bytes or bytearray, not list” error

The main reason for the “ValueError: The JSON object must be str, bytes or bytearray, not list” error is when we convert the list to JSON string, we use the loads() method. But the loads() method expects a string so in this case, the error will occur.

The loads() method is used to parse a valid JSON string, byte, or byte array and convert JSON data into a Python Dictionary. 

Example:

import json

seaCreatures = ['shark', 'cuttlefish', 'squid', 'mantis shrimp', 'anemone']
convertToJson = json.loads(seaCreatures)

print(convertToJson)

Output:

The error will happens if you try to run this code. 

Traceback (most recent call last):
  File "code.py", line 4, in <module>
  convertToJson = json.loads(seaCreatures)
TypeError: the JSON object must be str, bytes or bytearray, not list

How to fix the error “The JSON object must be str, bytes or bytearray, not list”

The best solution to solve the error “ValueError: The JSON object must be str, bytes or byte array, not list” is using the dumps() method to convert Python object into a JSON string to replace for loads() method, or you can use dumps() method to convert to string then use loads() method to convert to JSON string.

Solution 1: Use json.dumps() instead of json.loads()

You can easily fix this bug by using the dumps() method instead of json.loads().

The dumps() method allows convert a Python object into a JSON string.

Example:

import json

seaCreatures = ['shark', 'cuttlefish', 'squid', 'mantis shrimp', 'anemone']
convertToJson = json.dumps(seaCreatures)

print(convertToJson)

Output:

["shark", "cuttlefish", "squid", "mantis shrimp", "anemone"]

Solution 2: Use json.dumps() method to convert list to string then use json.loads()

The second solution to fix this error is using json.dumps() method to convert the list to the string then use json.loads(). Look at the following example to understand more:

import json

seaCreatures = ['shark', 'cuttlefish', 'squid', 'mantis shrimp', 'anemone']
convertToJson = json.dumps(seaCreatures)
loadConvertToJson = json.loads(convertToJson)

print(loadConvertToJson)

Output:

["shark", "cuttlefish", "squid", "mantis shrimp", "anemone"]

After doing that, your error will be resolved completely.

Summary

In this article, we already explained to you how to solve the error “ValueError: The JSON object must be str, bytes or byte array, not list” with two solutions use the dumps() instead of the loads() method and use the dumps() method to convert list to string then use the loads() method. We always hope this information will be usefull to you. Leave a comment below if you have any questions.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *