Keep A Datetime.date In 'yyyy-mm-dd' Format When Using Flask's Jsonify
Solution 1:
Following this snippet you can do this:
from flask.json import JSONEncoder
from datetime import date
classCustomJSONEncoder(JSONEncoder):
defdefault(self, obj):
try:
ifisinstance(obj, date):
return obj.isoformat()
iterable = iter(obj)
except TypeError:
passelse:
returnlist(iterable)
return JSONEncoder.default(self, obj)
app = Flask(__name__)
app.json_encoder = CustomJSONEncoder
Route:
import datetime as dt
@app.route('/', methods=['GET'])defindex():
now = dt.datetime.now()
return jsonify({'now': now})
Solution 2:
datetime.date
is not a JSON type, so it's not serializable by default. Instead, Flask adds a hook to dump the date to a string in RFC 1123 format, which is consistent with dates in other parts of HTTP requests and responses.
Use a custom JSON encoder if you want to change the format. Subclass JSONEncoder
and set Flask.json_encoder
to it.
from flask import Flask
from flask.json import JSONEncoder
classMyJSONEncoder(JSONEncoder):
defdefault(self, o):
ifisinstance(o, date):
return o.isoformat()
returnsuper().default(o)
classMyFlask(Flask):
json_encoder = MyJSONEncoder
app = MyFlask(__name__)
It is a good idea to use ISO 8601 to transmit and store the value. It can be parsed unambiguously by JavaScript Date.parse
(and other parsers). Choose the output format when you output, not when you store.
A string representing an RFC 2822 or ISO 8601 date (other formats may be used, but results may be unexpected).
When you load the data, there's no way to know the value was meant to be a date instead of a string (since date is not a JSON type), so you don't get a datetime.date
back, you get a string. (And if you did get a date, how would it know to return date
instead of datetime
?)
Solution 3:
You can change your app's .json_encoder
attribute, implementing a variant of JSONEncoder
that formats dates as you see fit.
Post a Comment for "Keep A Datetime.date In 'yyyy-mm-dd' Format When Using Flask's Jsonify"