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)
If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .

Comments

Leave a Reply