I am making a backend using Flask python. My Javascript file is fetching the python file, and as a response I want to send back all the lists in the list folder. Since I can't send back lists, I tried making this construction:
backsend_data = f"'count':'{len(name_value_list)}'"
list_count = 1
while list_count < len(name_value_list):
backsend_data = backsend_data + f"'name{list_count}':'{name_value_list[list_count]}','info{list_count}':'{info_value_list[list_count]}','class{list_count}':'{class_value_list[list_count]}'"
Everything works fine, but there is an error using the f-string:
Traceback (most recent call last):
File "/home/kon/.local/lib/python3.10/site-packages/flask/app.py", line 1498, in __call__
return self.wsgi_app(environ, start_response)
File "/home/kon/.local/lib/python3.10/site-packages/flask/app.py", line 1476, in wsgi_app
response = self.handle_exception(e)
File "/home/kon/.local/lib/python3.10/site-packages/flask_cors/extension.py", line 178, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/kon/.local/lib/python3.10/site-packages/flask/app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
File "/home/kon/.local/lib/python3.10/site-packages/flask/app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/kon/.local/lib/python3.10/site-packages/flask_cors/extension.py", line 178, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/kon/.local/lib/python3.10/site-packages/flask/app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
File "/home/kon/.local/lib/python3.10/site-packages/flask/app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File "/home/kon/Documents/Programming/Listed/src/backend/backend.pyw", line 69, in submit
return jsonify({backsend_data})
File "/home/kon/.local/lib/python3.10/site-packages/flask/json/__init__.py", line 170, in jsonify
return current_app.json.response(*args, **kwargs) # type: ignore[return-value]
File "/home/kon/.local/lib/python3.10/site-packages/flask/json/provider.py", line 214, in response
f"{self.dumps(obj, **dump_args)}\n", mimetype=self.mimetype
File "/home/kon/.local/lib/python3.10/site-packages/flask/json/provider.py", line 179, in dumps
return json.dumps(obj, **kwargs)
File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.10/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/usr/lib/python3.10/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/home/kon/.local/lib/python3.10/site-packages/flask/json/provider.py", line 121, in _default
raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
TypeError: Object of type set is not JSON serializable
I expected to see JSON back to Javascript, something like below:
{'count':'2', 'name1':'example1', 'info1':'example1', 'class1':'example1', 'name2':'example2', 'info2':'example2', 'class2':'example2'}
Instead of this:
backsend_data = f"'count':'{len(name_value_list)}'"
and this:
backsend_data = backsend_data + f"'name{list_count}':'{name_value_list[list_count]}','info{list_count}':'{info_value_list[list_count]}','class{list_count}':'{class_value_list[list_count]}'"
You can use this:
backsend_data = {'count':len(name_value_list)}
and this:
backsend_data[f'name{list_count}'] = name_value_list[list_count]
backsend_data[f'info{list_count}'] = info_value_list[list_count]
backsend_data[f'class{list_count}'] = class_value_list[list_count]
This will give you proper JSON objects