1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to overcome TypeError: Object of type datetime is not JSON serializable

Discussão em 'Python' iniciado por Stack, Setembro 7, 2025.

  1. Stack

    Stack Membro Participativo

    PreFile :

    PreFile = {
    "Preferences": {
    "Quotas": {
    "Rate limit": 0,
    "Reset time": "2020-10-28 10:57:21.482911"
    }
    }
    }


    My code:

    PrefFile["Preferences"]["Quotas"]["Reset time"] = data_option
    with open(Preferences_Path, 'w') as Preferences_File:
    json.dump(PrefFile, Preferences_File, indent=4, sort_keys=True)


    PreResetTime = PreFile["Preferences"]["Quotas"]["Reset time"]
    ResetTime = datetime.strptime(PreResetTime, '%Y-%m-%d %H:%M:%S.%f')


    Error output: TypeError: Object of type datetime is not JSON serializable

    File "main_functions.py", line 158, in updating_preferences
    json.dump(PrefFile, Preferences_File, indent=4, sort_keys=True)


    I'm going to use ResetTime later in the program

    if(ResetTime < (time_now - timedelta(hours=24))):
    code...

    Continue reading...

Compartilhe esta Página