[Python] How can I overcome "datetime.datetime not JSON serializable"?

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a basic dict as follows:

    sample = {}
    sample['title'] = "String"
    sample['somedate'] = somedatetimehere


    When I try to do jsonify(sample) I get:


    TypeError: datetime.datetime(2012, 8, 8, 21, 46, 24, 862000) is not JSON serializable
    ​

    What can I do such that my dictionary sample can overcome the error above?

    Note: Though it may not be relevant, the dictionaries are generated from the retrieval of records out of mongodb where when I print out str(sample['somedate']), the output is 2012-08-08 21:46:24.862000.

    Continue reading...

Compartilhe esta Página