Python – Get error datetime.date(2018, 6, 12) is not serializable JSON in Python

Get error datetime.date(2018, 6, 12) is not serializable JSON in Python… here is a solution to the problem.

Get error datetime.date(2018, 6, 12) is not serializable JSON in Python

I’m trying to use Flask-Ask and create Alexa skills. I’m having trouble storing the date and time in json

The error is as follows

raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.date(2018, 6, 12) is not JSON serializable

Below is a code snippet

@ask.intent("BookDateConfirmIntent")
def booking_confirmed(confirm_date):

start_date = session['attributes']['startDate']
    data = {'services': '1234a', 'startDate': start_date, 'message': 'booking confirmed'}
    print json.dumps(data, indent=4, sort_keys=True, default=str)

The date passed is similar to 2018-06-12

I

read that we need to serialize this, but I can’t get it to work as required by the above code. Someone please help. Thanks

Solution

You can try converting the DateTime Object type to string.

Change start_date to str(start_date).

Related Problems and Solutions