Skip to main content

How to Resolve "TypeError: Object of type function is not JSON serializable" in Python

When using Python's json library, you might encounter the TypeError: Object of type function is not JSON serializable. This error occurs when you pass a function object itself to json.dumps() instead of passing the return value of that function. The JSON format is a data-interchange format and has no concept of how to represent executable code like a Python function.

The most common cause of this error is a simple syntax mistake: forgetting to add parentheses () to call the function. This guide will explain the error and show you the straightforward fix.

Understanding the Error: Function Reference vs. Function Call

To understand this error, it's critical to know the difference between referring to a function and calling it:

  • Function Reference (my_function): When you use the name of a function without parentheses, you are referring to the function object itself. This is useful when passing functions as arguments to other functions (e.g., map(my_function, my_list)).
  • Function Call (my_function()): When you add parentheses after the function name, you are executing the function. The expression evaluates to the function's return value.

The json.dumps() method is designed to serialize data (like lists, dictionaries, strings, and numbers), not code. The TypeError is raised because you have passed it a function object, which it cannot convert into a JSON string.

Reproducing the TypeError

The error is easily triggered by passing the function name directly to json.dumps().

Example of code causing the error:

import json

def get_data():
"""This function returns a list, which IS JSON serializable."""
return [12, 25, 34]

# Incorrect: Passing the function object 'get_data' itself.
json_string = json.dumps(get_data)

Output:

Traceback (most recent call last):
File "main.py", line 8, in <module>
json_string = json.dumps(get_data)
File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type function is not JSON serializable

Solution: Call the Function with Parentheses ()

To fix this, you must call the function by adding parentheses () after its name. This executes the function, gets its return value (the list), and passes that serializable data to json.dumps().

Solution:

import json

def get_data():
"""This function returns a list, which IS JSON serializable."""
return [12, 25, 34]

# ✅ Correct: Calling the function to get its return value.
json_string = json.dumps(get_data())

print(json_string)
print(f"Type of the serialized data: {type(json_string)}")

Output:

[12, 25, 34]
Type of the serialized data: <class 'str'>

By simply changing get_data to get_data(), you provide json.dumps() with the list it expects, and the serialization succeeds.

Conclusion

The TypeError: Object of type function is not JSON serializable is a fundamental error that stems from a syntax mistake.

The ProblemThe Solution
You are passing a function reference (e.g., my_func) to json.dumps().You need to pass the function's return value by calling the function with parentheses (e.g., my_func()).

Always double-check that you have included the parentheses () when you intend to execute a function and use its result.