TypeError: Object of type function is not JSON serializable

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. TypeError: Object of type function is not JSON serializable
  2. TypeError: Object of type method is not JSON serializable

# TypeError: Object of type function is not JSON serializable

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.

main.py
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

object of type function is not json serializable

We forgot to call the get_employee() function in the call to the json.dumps() method.

# Call the function and serialize its output to solve the error

To solve the error, make sure to call the function and serialize its return value rather than the function itself.

main.py
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'>

call function and serialize output

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.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

The JSONEncoder class doesn't support function to JSON conversion by default.

Want to learn more about calling functions in Python? Check out these resources: How to call a Function N times in Python,Call a class method from another Class in Python.

If you got any of the following errors, click on the link to open the article:

# TypeError: Object of type method is not JSON serializable

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.

main.py
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

object of type method is not json serializable

We forgot to call the emp.get_salary() method in the call to the json.dumps() method.

# Call the method and serialize its return value to solve the error

To solve the error, make sure to call the method and serialize its return value rather than the method itself.

main.py
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'>

call method and serialize its return value

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.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev