Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set object is not JSON serializable [duplicate]

Tags:

python

json

set

When I try to run the following code:

import json  d = {'testing': {1, 2, 3}} json_string = json.dumps(d) 

I get the following exception:

Traceback (most recent call last):   File "json_test.py", line 4, in <module>     json_string = json.dumps(d)   File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps     return _default_encoder.encode(obj)   File "/usr/lib/python2.7/json/encoder.py", line 207, in encode     chunks = self.iterencode(o, _one_shot=True)   File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode     return _iterencode(o, 0)   File "/usr/lib/python2.7/json/encoder.py", line 184, in default     raise TypeError(repr(o) + " is not JSON serializable") TypeError: set([1, 2, 3]) is not JSON serializable 

What can I do to successfully use json.dumps with objects containing sets?

like image 313
user3398153 Avatar asked Mar 09 '14 10:03

user3398153


People also ask

Why is set not JSON serializable?

The Python "TypeError: Object of type set is not JSON serializable" occurs when we try to convert a set object to a JSON string. To solve the error, convert the set to a list before serializing it to JSON, e.g. json. dumps(list(my_set)) . Here is an example of how the error occurs.

How do I fix the object of type datetime is not JSON serializable?

The Python "TypeError: Object of type datetime is not JSON serializable" occurs when we try to convert a datetime object to a JSON string. To solve the error, set the default keyword argument to str in your call to the json. dumps() method.

Is not a JSON serializable?

The Python "TypeError: Object of type function is not JSON serializable" occurs when we try to serialize a function to JSON. To solve the error, make sure to call the function and serialize the object that the function returns.

Is not JSON serializable Python error?

The Python "TypeError: Object of type dict_items is not JSON serializable" occurs when we try to convert a dict_items object to JSON. To solve the error, convert the dict to JSON or pass the dict_items object to the list() class before the conversion to JSON.


2 Answers

Turn sets into lists before serializing, or use a custom default handler to do so:

def set_default(obj):     if isinstance(obj, set):         return list(obj)     raise TypeError  result = json.dumps(yourdata, default=set_default) 
like image 95
Martijn Pieters Avatar answered Sep 23 '22 12:09

Martijn Pieters


You can't fix it.

This error means just "json.dumps doesn't support data type "set".You should know JSON comes from javascript. And there is no data type like Python's "set" in javascript. So Python can't treat 'set' using JSON.

So you need another approach like @Martijn Pieters mentioned.


UPDATE

I forgot to say this.

If you want to dump "set" or any other python object that is not supported JSON, you can use pickle or cPickle module. If you use the "dump.txt" only from Python, this may be helpful.

import cPickle  d = {'testing': {1, 2, 3}}  #dump with open("pickledump.txt", "w") as fp:     cPickle.dump(d, fp)  #load with open("pickledump.txt", "r") as fp:     x = cPickle.load(fp) 
like image 42
Kei Minagawa Avatar answered Sep 26 '22 12:09

Kei Minagawa