AppDividend
Latest Code Tutorials

How to Convert Python Set to JSON Data type

0

The set data type in Python is not JSON Serializable. If you try to convert the set to json, you will get this error: TypeError: Object of type set is not JSON serializable. The inbuilt Python json module can only handle primitives data types with a direct JSON equivalent and not complex data types like Set.

For example, the basic problem is that the JSON encoder methods like json.dump() and json.dumps() simply knows how to serialize the basic data types by default (e.g., dictionary, lists, strings, integers, None, etc.). Let’s see how to resolve this error.

Convert Python Set to JSON

To convert Python Set to JSON, use the third-party module like jsonpickle to make the set JSON serializable. The jsonpickle is a third-party Python module created to work with complex Python Objects.

You can use the jsonpickle module to serialize complex objects into JSON and deserialization from JSON to complex Python objects. In our case, it is converting a Python set to json and json to set.

The jsonpickle module allows more complex data structures to be serialized to JSON. The jsonpickle is module is highly configurable and extendable.

Follow the below steps to convert the set to json string.

Step 1: Install jsonpickle module

To work with the jsonpickle module, you need first to install it in your system.

python3 -m pip install jsonpickle

Step 2: Import the jsonpickle module

To use the jsonpickle module in Python, use the import statement.

import jsonpickle

Step 3: Define a set and print its values and data type.

To define a set in Python, use the curly braces, and put the comma-separated values inside the brackets.

import jsonpickle

set_data = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}
print(set_data)
print(type(set_data))

Step 4: Use the jsonpickle.encode() method convert into json

The jsonpickle.encode() method is used to transform the object into a JSON string.

json_data = jsonpickle.encode(set_data)

Now, print the value of json_data and its data type.

import jsonpickle

set_data = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}
print(set_data)
print(type(set_data))
print("----------After converting set to json-----------")
json_data = jsonpickle.encode(set_data)
print(json_data)
print(type(json_data))

Output

{'Isaac', 'Vlad Tepes', 'Trevor', 'Alucard'}
<class 'set'>
----------After converting set to json-----------
{"py/set": ["Isaac", "Vlad Tepes", "Trevor", "Alucard"]}
<class 'str'>

You can see that we got the json string after using the jsonpickle.encode() method.

Convert JSON to Set using jsonpickle.decode() method

To recreate the Python set from JSON String, use the jsonpickle.decode() method.

import jsonpickle

set_data = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}
print(set_data)
print(type(set_data))
print("----------After converting set to json-----------")
json_data = jsonpickle.encode(set_data)
print(json_data)
print(type(json_data))
print("----------After converting json to set-----------")
decoded_set = jsonpickle.decode(json_data)
print(decoded_set)
print(type(decoded_set))

Output

{'Vlad Tepes', 'Trevor', 'Isaac', 'Alucard'}
<class 'set'>
----------After converting set to json-----------
{"py/set": ["Vlad Tepes", "Trevor", "Isaac", "Alucard"]}
<class 'str'>
----------After converting json to set-----------
{'Vlad Tepes', 'Trevor', 'Isaac', 'Alucard'}
<class 'set'>

And we get our original Python object, which is a Set, and we have verified that by checking its data type.

There is one more approach that can make Set serializable, but in that, we need to build an encoder from scratch using json and JSONEncoder library.

Using custom JSONEncoder

To convert Set to JSON String, write a custom encoder to serialize Python Set to JSON. Python json module provides a JSONEncoder to encode Python objects to JSON. We can extend by implementing its default() method that can convert JSON serializable set.

The JSONEncoder class has a default() method, which will be used to execute a JSONEncoder.encode() method. This method converts only basic types into JSON.

Follow the below steps to create a custom JSONEncoder and transform the set into a json string.

Step 1: Import required modules

We will use the two methods of the json module. So import two methods at the starting of the file.

from json import dumps
from json import JSONEncoder

Step 2: Define a custom JSONEncoder

Let’s define the class set_encoder.

class set_encoder(JSONEncoder):
    def default(self, obj):
        return list(obj)

Using this class, now we can use the encode() method to convert the set into a json string.

Step 3: Define a sample set and use the encode() method.

Let’s define a sample set, which we will then convert.

set_data = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}

Now, we will use the set_encoder().encode() method and pass the set_data as a parameter which returns the json string. So our full code looks like this.

from json import dumps
from json import JSONEncoder


class set_encoder(JSONEncoder):
    def default(self, obj):
        return list(obj)


set_data = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}
print(set_data)
print("----------After converting set to json-----------")
json_data = set_encoder().encode(set_data)
print(json_data)
print(type(json_data))

Output

{'Trevor', 'Alucard', 'Vlad Tepes', 'Isaac'}
----------After converting set to json-----------
["Trevor", "Alucard", "Vlad Tepes", "Isaac"]
<class 'str'>

That is it. We have successfully built an encoder that can convert any object into a json string.

There is another way of using a custom json encoder with the dumps() method. Let’s see how to do that.

The json.dumps() method can accept a custom encoder as an argument and returns the encoded string.

from json import dumps
from json import JSONEncoder


class set_encoder(JSONEncoder):
    def default(self, obj):
        return list(obj)


set_data = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}
print(set_data)
print("----------Using dumps() method to convert set to json-----------")
json_data = dumps(set_data, indent=4, cls=set_encoder)
print(json_data)
print(type(json_data))

Output

{'Vlad Tepes', 'Isaac', 'Alucard', 'Trevor'}
----------Using dumps() method to convert set to json-----------
[
    "Vlad Tepes",
    "Isaac",
    "Alucard",
    "Trevor"
]
<class 'str'>

And we get the same result with, of course, indentation.

Convert JSON String to Set using a custom decoder

To convert back from json string to Python set, use the combination of json.loads() and set() method.

from json import dumps, JSONEncoder, loads


class set_encoder(JSONEncoder):
    def default(self, obj):
        return list(obj)


set_data = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}
print(set_data)
print("----------Using dumps() method to convert set to json-----------")
json_data = dumps(set_data, indent=4, cls=set_encoder)
print(json_data)
print(type(json_data))
print("----------Using set(loads()) method to convert json to set-----------")
set_obj = set(loads(json_data))
print(set_obj)
print(type(set_obj))

Output

{'Trevor', 'Vlad Tepes', 'Alucard', 'Isaac'}
----------Using dumps() method to convert set to json-----------
[
    "Trevor",
    "Vlad Tepes",
    "Alucard",
    "Isaac"
]
<class 'str'>
----------Using set(loads()) method to convert json to set-----------
['Trevor', 'Vlad Tepes', 'Alucard', 'Isaac']
<class 'set'>

Output

{'Alucard', 'Vlad Tepes', 'Trevor', 'Isaac'}
----------Using dumps() method to convert set to json-----------
[
    "Alucard",
    "Vlad Tepes",
    "Trevor",
    "Isaac"
]
<class 'str'>
----------Using set(loads()) method to convert json to set-----------
{'Alucard', 'Vlad Tepes', 'Trevor', 'Isaac'}
<class 'set'>

The loads() method returns the list, and we can convert a list to set using the set() method.

That is it for this tutorial.

See also

Python set to dict

Python set to string

Python set to list

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.