Question

[Solved] Python json.dumps TypeError: Object of type ‘set’ is not JSON serializable , when trying convert from variable , working when hardcoded

i try to create JSON string from variables in python but I get strange behaver

this result good formating when i hardcode the pro object:

rev_= 'Package ID: bbbbbnBuildnnumber: 154nBuiltn'
s_ver_str_ = 'bbb.bbb.3.3.98'
result = json.dumps({"rev": rev_, "s_ver_str": s_ver_str_, "pro": {'message': 'no verstion found'}})

but this return error when i try to set the pro object into variable :

    rev_= 'Package ID: bbbbbnBuildnnumber: 154nBuiltn'
    s_ver_str_ = 'bbb.bbb.3.3.98'
    pro_ = "'message': 'no verstion found'"
    result = json.dumps({"rev":rev_,"s_ver_str":s_ver_str_,"pro": { pro_ }})

getting this error:

Traceback (most recent call last):
  File "C:Program FilesJetBrainsPyCharm Community Edition 2019.3.2pluginspython-cehelperspydevpydevd.py", line 1434, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:Program FilesJetBrainsPyCharm Community Edition 2019.3.2pluginspython-cehelperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"n", file, 'exec'), glob, loc)
  File "C:/Dev/python/new_tester/simple_main.py", line 19, in <module>
    result = json.dumps({"rev":rev_,"s_ver_str":s_ver_str_,"pro": { pro_ }})
  File "C:Python36-64libjson__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:Python36-64libjsonencoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:Python36-64libjsonencoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:Python36-64libjsonencoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'set' is not JSON serializable

Solution #1:

Your problem is that your code doesn’t do what you expect it to do.
the curly brackets in Python are used for both dict and set objects.

if you are using them like that:

{'message': 'Hello World'}

an object of type dict is created.

However, if you use them like that:

{'Hello World'}

an object of type set is created.

Your pro variable, is a single str variable, therefore, doing the following:

pro_ = {"'message': 'no verstion found'"}

would create a set with one value only – your string.


If you would like to serialize that set into the json, you can do it with list(_pro) but that wouldn’t really make any sense, as it will also contain your quotation marks.

Respondent: Or Y

Solution #2:

pro_ is a string literal while you want it to be a dictionary like in your first example.

rev_= 'Package ID: bbbbbnBuildnnumber: 154nBuiltn'
s_ver_str_ = 'bbb.bbb.3.3.98'
pro_ = {"message": "no verstion found"}
result = json.dumps({"rev":rev_,"s_ver_str":s_ver_str_,"pro": pro_ })
Respondent: susu

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy