S
S
sergeyfilippov42019-08-06 18:27:24
Python
sergeyfilippov4, 2019-08-06 18:27:24

Error in json dump, what to do?

Gives an error object of type set not JSON serializable

import json

# Make it work for Python 2+3 and with Unicode
import io
try:
    to_unicode = unicode
except NameError:
    to_unicode = str

# Define data
data = {'users':{'user','user2'}}
# Write JSON file
with io.open('data.json', 'w', encoding='utf8') as outfile:
    str_ = json.dumps(data,
                      indent=4, sort_keys=True,
                      separators=(',', ': '), ensure_ascii=False)
    outfile.write(to_unicode(str_))

# Read JSON file
with open('data.json') as data_file:
    data_loaded = json.load(data_file)
print(data_loaded)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly, 2019-08-06
@sergeyfilippov4

An array is specified in square brackets, unlike a dictionary.
data = {'users': ['user', 'user2']}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question