0

I have a dictionary of set elements like below, that is "a" is a set of c and e. How can I convert it into a json object?

my_data = { "a" : {"c", "e"},
              "b" : {"c", "e"},
              "c" : {"a", "b", "d", "e"},
              "d" : {"c"},
              "e" : {"c", "b"},
              "f" : {},
              "g" : {"a", "s"}

            }

json.dumps will give the following error,

TypeError: Object of type set is not JSON serializable

Thank you in advance.

Droid-Bird
  • 1,295
  • 5
  • 15
  • 34
  • Apparently we cannot convert a set to json. So need to convert the child-dictionary to list. And then convert the dictionary to json. As follows, << def dict_of_set_to_json(dictionary): new_dict = {} for element in dictionary.keys(): new_dict[element] = list(graph[element]) return new_dict >> – Droid-Bird Jan 21 '22 at 11:48

0 Answers0