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

by | Programming, Python, Tips

This error occurs when you try to serialize a set object to a JSON string using the json.dumps() method. You can solve this error by converting the set to a list using the built-in list() function and passing the list to the json.dumps() method. For example,

json_str = json.dumps(list(a_set))

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


TypeError: Object of type set 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 set” tells us the error is due to an illegal operation with a set object.

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 set type.

Example

Let’s look at an example of serializing a set using the json.dumps() method.

import json

a_set = {2, 4, 6, 8, 10}

json_str = json.dumps(a_set)

print(json_str)

Let’s run the code to see the result:

TypeError: Object of type set is not JSON serializable

The error occurs because the set type is one of the possible data types to serialize.

Solution #1: Convert set to list

The simplest way to solve this error is to convert the set to a list using the built-in list() function. The list data type is JSON serializable. Let’s look at the revised code:

import json

a_set = {2, 4, 6, 8, 10}

json_str = json.dumps(list(a_set))

print(json_str)
print(type(json_str))

Let’s run the code to see the JSON string and confirm its type:

[2, 4, 6, 8, 10]
<class 'str'>

Solution #2: Define a custom function for default kwarg

We can also solve this error by defining a custom function that converts the set to a list and passing this function to json.dumps() as the default keyword argument. The default value for the keyword argument default is None. We can set default to a function that gets called for objects that are not serializable to convert them to a serializable type.

import json
def serialize_sets(obj):
    if isinstance(obj, set):
        return list(obj)
    raise TypeError ("Type %s is not serializable" % type(obj))

The custom function checks if the object is a set and then converts it to a list. Otherwise, it raises a TypeError. Let’s set the default keyword to our custom function and run the code:

a_set = {2, 4, 6, 8, 10}

json_str = json.dumps(a_set, default=serialize_sets)

print(json_str)
print(type(json_str))
[2, 4, 6, 8, 10]
<class 'str'>

Solution #3: Define a JSONEncoder subclass for the cls kwarg

The third way we can solve this error is by building a custom JSONEncoder subclass. This subclass will override the default method to serialize additional types. Similar to the custom function, the default method checks if the object is a set and converts it to a list.

import json
class SetEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, set):
            return list(obj)
        return json.JSONEncoder.default(self, obj)

We have to specify the custom JSONEncoder subclass with the cls keyword argument. Otherwise, JSONEncoder is used.

a_set = {2, 4, 6, 8, 10}

json_str = json.dumps(a_set, cls=SetEncoder)

print(json_str)
print(type(json_str))

Let’s run the code to see the result.

[2, 4, 6, 8, 10]
<class 'str'>

Below is the collection of objects that the JSONEncoder class supports and their JSON equivalent

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int- & float- derived Enumsnumber
Truetrue
Falsefalse
Nonenull
JSONEncoder Supported objects and types by default

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 ✨