How to overcome "datetime.datetime not JSON serializable"?

One way to overcome "datetime.datetime not JSON serializable" in Python is to use the json.dumps() method with the default argument, default=str, which converts the datetime object to a string before serializing. Another way is to convert datetime object to a json-serializable format like a string in isoformat.

Here is an example:

import json
from datetime import datetime

# Create a datetime object
now =

# Serialize the object to a JSON formatted string
json_str = json.dumps(now.isoformat())


Watch a course Python - The Practical Guide

You can also use dateutil library's dateutil.parser.parse() function to convert string to datetime.

from datetime import datetime
from dateutil.parser import parse

class DateTimeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, (datetime, date)):
            return obj.isoformat()
        return json.JSONEncoder.default(self, obj)

data = {'date':}
json.dumps(data, cls=DateTimeEncoder)