. Advertisement .
..3..
. Advertisement .
..4..
Did you get TypeError: Object of type set is not JSON serializable while working on a Python Project? It is quite normal to encounter TypeError, especially when you are just starting out with Python Programming. You just need to know that getting a TypeError warning on your programming screen is what many programmers get. The important thing is how you tackle the error and your determination to get it through.
This type error warning comes when trying to convert the ‘set’ object to a JSON string. There are ways to get through this error message, let’s check out
How to Fix TypeError: Object of type set is not JSON serializable
Whenever you convert Python set to the JSON, you received the TypeError: Object of type set is not JSON serializable. To solve the TypeError, before converting ‘set’ to JSON, you need to convert ‘set’ to a list. Have a look at the example of how you get the error
import json
list1 = {'a', 'b', 'c', 'd', 'e', 'f'}
json_string = json.dumps(list1)
Error message
TypeError: Object of type set is not JSON serializable
Here, we have tried to pass an object ‘set’ to the ‘json.dumps()
’ method. But, by default, this method doesn’t handle ‘set’.
Using list()
The simplest way to solve type error is the list(), which is a built-in function. This function makes the data JSON serialized. Let’s have a look at the code:
import json
list2 = {12, 29, 22, 28, 19, 15, 18}
json_string = json.dumps(list(list2))
print(json_string)
print(type(json_string))
Output
[18, 19, 29, 22, 12, 28, 15]
<class 'str'>
As you can see, the ‘list’ values can be handled by the default JSON encoder, which means a native Python ‘list’ can be used when serializing to JSON instead of a ‘set’.
Meanwhile, the conversion from a Python object to a JSON string is possible with the json.dumps.
Using JSONEncoder
It is a class that is used with a ‘default’ method to handle the conversion. Check the code out
import json
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)
list3 = {'Coconut', 'Pineapple', 'Durian', 'Pear', 'Cherry'}
json_string = json.dumps(list3, cls=SetEncoder)
print(json_string)
print(type(json_string))
Output
["Pear", "Coconut", "Durian", "Cherry", "Pineapple"]
<class 'str'>
It is to notice that the class ‘JSONEncoder’ doesn’t handle ‘set’ to JSON conversion by default.
This can be handled by the default()
method to get a serialized object in return. Like this,
import json
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)
The isinstance being a function return True, if passed in object is a subclass of passed in class or passed in object. This is how we do the serialization.
Using JSONEncoder for the cls Kwarg
This is also a way of solving the error where we can build a custom JSONEncoder subclass, which just overrides the default method to serialize. Check an example
import json
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)
list4 = {5, 10, 15, 20, 25}
json_string = json.dumps(list4, cls=SetEncoder)
print(json_string)
print(type(json_string))
Output
[20, 5, 25, 10, 15]
<class 'str'>
Conclusion
If you have read till the end, you understand how to get rid of TypeError: Object of type set is not JSON serializable. Hope it help!
Leave a comment