Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Ivana Cajina
The Python "TypeError: the JSON object must be str, bytes or bytearray, not
Response" occurs when we pass a Response
object to the json.loads()
method.
To solve the error, call the json()
method on the Response
object instead,
e.g. result = res.json()
.
Here is an example of how the error occurs.
import json import requests def make_request(): res = requests.get('https://reqres.in/api/users') # ⛔️ TypeError: the JSON object must be str, bytes or bytearray, not Response parsed = json.loads(res) make_request()
We passed a Response
object to the json.loads()
method which caused the
error.
To solve the error, use the json()
method on the response object instead.
import requests def make_request(): res = requests.get('https://reqres.in/api/users') # ✅ call .json() method on Response object parsed = res.json() print(parsed) print(type(parsed)) # 👉️ <class 'dict'> make_request()
json()
method on the Response
object to parse it into a native Python object before accessing any of its keys.You should use the json()
method to parse the data from all requests, not just
HTTP GET
.
Here is an example of a POST request with the requests
module.
import requests def make_request(): res = requests.post( 'https://reqres.in/api/users', data={'name': 'John Smith', 'job': 'manager'} ) # ✅ parse JSON response to native Python object data = res.json() # 👇️ {'name': 'John Smith', 'job': 'manager', 'id': '649', 'createdAt': '2022-05-20T10:11:23.939Z'} print(data) print(data['name']) # 👉️ "John Smith" print(data['job']) # 👉️ "manager" print(data['id']) # 649 make_request()
If you are working with a JSON string or a native Python object, make sure to
use the json.loads()
and json.dumps()
methods.
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.
The json.dumps method converts a Python object to a JSON formatted string.
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.