from flask.json import JSONEncoder import datetime import decimal class JSONEncoder(JSONEncoder): def default(self, obj): try: if isinstance(obj, datetime.datetime): return obj.strftime('%Y-%m-%d %H:%M:%S') elif isinstance(obj, decimal.Decimal): return float(obj) iterable = iter(obj) except TypeError: pass else: return list(iterable) return JSONEncoder.default(self, obj)