How to Solve Python TypeError: Object of type function is not JSON serializable

by | Programming, Python, Tips

This error occurs when you pass the name of a function to the json.dumps() method instead of the function call. You can pass a function to json.dumps() that returns a JSON serializable object. You can solve this error by putting parentheses after the function name to call the function. For example,

json_str = json.dumps(function_name())

This tutorial will go through how to solve the error with code examples.


TypeError: Object of type function is not JSON serializable

Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type.

The part “Object of type function” tells us the error is due to an illegal operation with a function.

Serialization in Python refers to converting a Python object into a transmittable format that we can recreate when needed using deserialization. JSON serialization returns a human-readable string form called a JSON string.

The JSON encoder json.dump() and json.dumps() can only serialize certain object types like dictionaries, lists, or strings.

is not JSON serializable” informs us that the JSON serialization is an illegal operation for the function type.

Example

Let’s look at an example of serializing a list of prime numbers returned by a function. First, we will define the function as follows:

import json

def get_prime_numbers():

    num_lists = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

    return num_lists

Next, we will attempt to pass the function to the json.dumps() method to serialize the list.

json_str = json.dumps(get_prime_numbers)

print(json_str)

Let’s run the code to see the result:

TypeError: Object of type function is not JSON serializable

The error occurs because we passed the function when calling json.dumps() instead of the function call.

If we do not put parentheses () after the variable name get_prime_numbers, the object is a function.

If we put parentheses after get_prime_numbers, we call the function, which returns a list.

We can verify the difference between a function and a function call using the type() function as follows:

print(type(get_prime_numbers))
print(type(get_prime_numbers()))
<class 'function'>
<class 'list'>

Solution

We can solve the error by passing the function call to json.dumps().

The function call returns the list of integers, which is serializable.

Let’s look at the updated code:

json_str = json.dumps(get_prime_numbers())

print(json_str)

Let’s run the code to get the result:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

We successfully serialized the list to a JSON string.

Summary

Congratulations on reading to the end of this tutorial.

For further reading on errors involving JSON serialization, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee