Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Chris Yang
The Python "TypeError: the JSON object must be str, bytes or bytearray, not
dict" occurs when we pass a dictionary to the json.loads()
method. To solve
the error, remove the call to json.loads()
and use the json.dumps()
method
if trying to convert the dict to a JSON string.
Here is an example of how the error occurs.
import json my_dict = {'name': 'Alice', 'age': 30} # ⛔️ TypeError: the JSON object must be str, bytes or bytearray, not dict result = json.loads(my_dict)
The json.loads method parses a JSON string into a native Python object.
json.loads()
method.If you need to convert a dictionary to a JSON string, use the json.dumps()
method.
import json my_dict = {'name': 'Alice', 'age': 30} my_json_str = json.dumps(my_dict) print(my_json_str) # 👉️ '{"name": "Alice", "age": 30}' print(type(my_json_str)) # 👉️ <class 'str'>
The json.dumps method converts a Python object to a JSON formatted string.
In other words, 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.
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.
If you need to read from a json file, use the json.load()
method.
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(my_data['name']) # 👉️ 'Alice' print(type(my_data)) # 👉️ <class 'dict'>
The code sample above assumes that you have an example.json
file in the same
directory.
{"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.
The JSONEncoder
class supports the following objects and types by default.
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str | string |
int, float, int and float derived Enums | number |
True | true |
False | false |
None | null |
If you aren't sure what type of object a variable stores, use the built-in
type()
class.
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.