The jsonify
function in flask seems to return strings for all Decimal
values instead of floats. Is there a builtin way to go around this?
In the meantime, I've had to manually remap it, but would like to avoid this if possible
from decimal import Decimal
result = {
k: (float(v) if isinstance(v, Decimal) else v)
for k, v in result.items()
}
return jsonify(result)
CodePudding user response:
I dont get this error, what exactly are you trying to return?
from flask import jsonify
from decimal import Decimal
v = Decimal(0.1)
return jsonify({"0.1": float(v)})
-->
{
"0.1": 0.1
}
without conversion:
v = Decimal(0.1)
return jsonify({"0.1": v})
{
"0.1": 0.1000000000000000055511151231257827021181583404541015625
}
Also, make sure you check the "raw data" of the return, not the prettyprinted or some other format that your browser might use.
CodePudding user response:
Solution: just use FastAPI instead