Skip to content Skip to sidebar Skip to footer

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: bbbbb\nBuild\nnumber: 1

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.

Solution 2:

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

rev_= 'Package ID: bbbbb\nBuild\nnumber: 154\nBuilt\n'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_ })

Post a Comment for "Python Json.dumps Typeerror: Object Of Type 'set' Is Not Json Serializable , When Trying Convert From Variable , Working When Hardcoded"