Convert decimal to string for json file
import json
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
# 👇️ if passed in object is instance of Decimal
# convert it to a string
if isinstance(obj, Decimal):
return str(obj)
# 👇️ otherwise use the default behavior
return json.JSONEncoder.default(self, obj)
def write_json_data(data, filename):
with open("{}.json".format(filename), "w", encoding="utf-8") as f:
# json.dump(data, f, ensure_ascii=False, indent=4, cls=DecimalEncoder)
json.dump(data, f, ensure_ascii=False, cls=DecimalEncoder)