JSON serializable error

24 views
Skip to first unread message

Anusha Kommineni

unread,
Aug 3, 2018, 10:19:44 AM8/3/18
to django...@googlegroups.com
Hi,
    I am getting below error.Can anyone help me?


TypeError: datetime.datetime(2018, 2, 12, 0, 0, tzinfo=<UTC>) is not JSON serializable

Julio Biason

unread,
Aug 3, 2018, 10:26:54 AM8/3/18
to django...@googlegroups.com
Hi Anusha,

It seems you're trying to generate a JSON of some object/dictionary with a Datetime in it. Datetimes are not serializable 'cause there is no date/time representation in JSON (maybe epoch, but that's not the default).

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACBpiKjjH6WhtHfc%3DqLzQ7v5Dc7jDNuAmkRnzB4kb9rzE_Rr1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

vineeth sagar

unread,
Aug 3, 2018, 10:49:16 AM8/3/18
to django...@googlegroups.com
You can use unix timestamp, It's json serializable and can be converted to a datetime object. If I am not wrong you were trying to store it in a session? try this.

import time

timestamp=time.time()
request.session['current time']= timestamp
import datetime

datetime.datetime.fromtimestamp(request.session['current time'])

Christophe Pettus

unread,
Aug 3, 2018, 12:25:36 PM8/3/18
to django...@googlegroups.com

> On Aug 3, 2018, at 07:18, Anusha Kommineni <anushakom...@gmail.com> wrote:
> TypeError: datetime.datetime(2018, 2, 12, 0, 0, tzinfo=<UTC>) is not JSON serializable

The error means what it says; there is no default way of serializing a datetime into JSON. You can use the DjangoJSONEncoder, which does allow serializing datetimes:

https://docs.djangoproject.com/en/2.0/topics/serialization/#djangojsonencoder

--
-- Christophe Pettus
x...@thebuild.com

Reply all
Reply to author
Forward
0 new messages