Python json encode datetime

Fix TypeError: datetime.datetime is not JSON serializable error.

import datetime
import json
 
from django.core.serializers.json import DjangoJSONEncoder
 
print json.dumps({
    'timestamp': datetime.datetime.now(),
}, cls=DjangoJSONEncoder, indent=2)
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment