Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python object of type set is not json serializable

Use the jsonpickle module to make Python set JSON serializable
Steps:
    1- Install jsonpickle using pip. pip install jsonpickle
    2- Execute jsonpickle.encode(object) to serialize custom Python Object.
Example:
import json
import jsonpickle
from json import JSONEncoder
sampleSet = {25, 45, 65, 85}

print("Encode set into JSON using jsonpickle")
sampleJson = jsonpickle.encode(sampleSet)
print(sampleJson)

# Pass sampleJson to json.dump() if you want to write it in file

print("Decode JSON into set using jsonpickle")
decodedSet = jsonpickle.decode(sampleJson)
print(decodedSet)

# to check if we got set after decoding
decodedSet.add(95)
print(decodedSet)
Source by pynative.com #
 
PREVIOUS NEXT
Tagged: #python #object #type #set #json #serializable
ADD COMMENT
Topic
Name
6+2 =