How To Fix TypeError: Object of type set is not JSON serializable in Python

Many developers try to serialize a set to JSON, but they fail. The error message: TypeError: Object of type set is not JSON serializable keeps showing up. This problem is easy to fix. We explain the cause and give the solutions right below. Scroll down to read!

Cause of TypeError: Object of type set is not JSON serializable in Python

The problem occurs when you try to convert the Python set to JSON. For example, you use the method json.dumps() to transform the Python data to JSON-formatted strings. But the error message: TypeError: Object of type set is not JSON serializable comes up.

Here is the code sample that leads to the issue:

import json

# Create a set in Python
testSet = {"Learn", "Share", "It"}

# Serialize a set to JSON
print(json.dumps(testSet))

The error message will show up:

    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializable

Please remember that JSON and Python don’t have the same type of data. The serializable data types in Python that JSON supports are dict, tuple, list, string, integer, long, float, True, False, and None.

As you see, JSON does not support conversion from the Python set. That’s why the error message comes up.

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

Here is how you fix the TypeError: Object of type set is not JSON serializable.

Switch the Python set to a different data type that is JSON serializable

As mentioned, JSON does not support serializing a Python set. That’s why you need to convert the set to a JSON serializable data type.

The syntax is simple like this:

[data type]([set])

You write the data type and put your Python set in the () to start the conversion.

Since the conversion is completed, you can start serializing to JSON data format.

The code sample below transforms the Python set to a Python tuple before JSON serialization. See the code:

import json

# Create a set in Python
testSet = {"Learn", "Share", "It"}
newSet = tuple(testSet)

# Serialize a set to JSON
print(json.dumps(newSet))

Please remember that the Python set is a collection that has no order. Therefore, the output will be different each time you run the code. But the sure thing is that the output will contain all the elements in the set. 

Here is a sample of the output:

["Learn", "It", "Share"]

Use the “default” of the json.dumps() method

The json.dumps() method has the default feature, which helps you treat all data that is non-serializable to the decided serializable type. 

The syntax is like this:

json.dumps([set], default="[serializable data]")

For example, you put the list as [serializable data]. The Python set will automatically be treated as a list, which is JSON serializable.

Please notice that the default only treats non-serializable data. If the data is already serializable, it will not be converted to the default data anymore.

Here is the code sample:

import json

# Create a set in Python
testSet = {"Learn", "Share", "It"}

# Serialize a set to JSON
print(json.dumps(testSet, default=list))

The output will be:

["It", "Learn", "Share"]

As mentioned, the output can be different each time you execute the code, because the elements in the Python set are unordered.

Summary

In conclusion, the TypeError: Object of type set is not JSON serializable occurs when you try to convert the Python set to JSON data format. Unfortunately, JSON does not support the data type set. You have to convert the set to serializable data before the JSON conversion. We have shown you 2 ways to fix this issue. You can decide the way that is the most convenient for your programming!

Leave a Reply

Your email address will not be published. Required fields are marked *