datetime.date(2014, 4, 25) is not JSON serializable in Django [duplicate]
You can also do this:
def date_handler(obj): return obj.isoformat() if hasattr(obj, 'isoformat') else objprint json.dumps(data, default=date_handler)
From here.
Update as per J.F.Sebastian comment
def date_handler(obj): if hasattr(obj, 'isoformat'): return obj.isoformat() else: raise TypeErrorprint json.dumps(data, default=date_handler)
Convert date to equivalent iso format,
In [29]: datetime.datetime.now().isoformat()Out[29]: '2020-03-06T12:18:54.114600'
See the Extending encoder section from the json package docs https://docs.python.org/2/library/json.html
I have used this method and found it quite effective. I think this is what you are looking for.
import jsonclass DatetimeEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime): return obj.strftime('%Y-%m-%dT%H:%M:%SZ') elif isinstance(obj, date): return obj.strftime('%Y-%m-%d') # Let the base class default method raise the TypeError return json.JSONEncoder.default(self, obj)json.dumps(dict,cls=DatetimeEncoder)