Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)

I want to send a datetime.datetime object in serialized form from Python using JSON and deserialize in JavaScript using JSON. What is the best way to do this?

1 Answer

0 votes
by (106k points)

To send a datetime.datetime object in serialized form from Python using JSON and deserialize in JavaScript using JSON. You can add the 'default' parameter to json.dumps to handle this:

date_handler = lambda obj:(

obj.isoformat()

if isinstance(obj, (datetime.datetime, datetime.date))

else None

)

json.dumps(datetime.datetime.now(), default=date_handler) 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...