Last updated: Apr 8, 2024
Reading timeยท3 min
The Python "TypeError: Object of type function is not JSON serializable" occurs when we try to serialize a function to JSON.
To solve the error, make sure to call the function and serialize the object that the function returns.
Here is an example of how the error occurs.
import json def get_employee(): return {'name': 'Bobby Hadz', 'age': 30} # โ๏ธ TypeError: Object of type function is not JSON serializable json_str = json.dumps(get_employee) # ๐๏ธ forgot to call function
We forgot to call the get_employee()
function in the call to the
json.dumps()
method.
To solve the error, make sure to call the function and serialize its return value rather than the function itself.
import json def get_employee(): return {'name': 'Bobby Hadz', 'age': 30} # โ Call the function get_employee() and NOT get_employee json_str = json.dumps(get_employee()) print(json_str) # ๐๏ธ '{"name": "Bobby Hadz", "age": 30}' print(type(json_str)) # ๐๏ธ <class 'str'>
The json.dumps() method converts a Python object to a JSON formatted string.
We called the function, so we serialized the dict
object rather than trying to
serialize the function itself.
You have to add parentheses to call a function, e.g. get_employee()
, otherwise
we pass a reference to the get_employee
function to the json.dumps
method
which is not what we want.
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 |
The JSONEncoder
class doesn't support function
to JSON conversion by
default.
If you got any of the following errors, click on the link to open the article:
The Python "TypeError: Object of type method is not JSON serializable" occurs when we try to serialize a method to JSON.
To solve the error, make sure to call the method and serialize the object that the method returns.
Here is an example of how the error occurs.
import json class Employee(): def get_salary(self): return 100 emp = Employee() # โ๏ธ TypeError: Object of type method is not JSON serializable json_str = json.dumps({'salary': emp.get_salary}) # ๐๏ธ forgot to call method
We forgot to call the emp.get_salary()
method in the call to the
json.dumps()
method.
To solve the error, make sure to call the method and serialize its return value rather than the method itself.
import json class Employee(): def get_salary(self): return 100 emp = Employee() # โ call method emp.get_salary() and NOT emp.get_salary json_str = json.dumps({'salary': emp.get_salary()}) print(json_str) # ๐๏ธ '{"salary": 100}' print(type(json_str)) # ๐๏ธ <class 'str'>
The json.dumps() method converts a Python object to a JSON formatted string.
We called the method, so we serialized the int, rather than trying to serialize the method itself.
You have to add parentheses to call a method, e.g. emp.get_salary()
, otherwise
we pass a reference to the emp.get_salary
method to the json.dumps
method
which is not what we want.
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 |
The JSONEncoder
class doesn't support method
to JSON conversion by default.
I've also written an article on how to check if a variable is a function.
You can learn more about the related topics by checking out the following tutorials: