I created a dictionary and was trying to dump it as a json file, but had problems due to the type of the key being a datetime.
In my researches I found solutions for the values but not for the keys.
Heres the code I have for now:
import json
from datetime import date, datetime
d = {
datetime.now(): {
'name' : 'Foo'
}
}
def myconverter(o):
if isinstance(o, datetime):
return o.isoformat()
print(d)
print(json.dumps(d, default=myconverter, indent=4))
Error message: TypeError: keys must be str, int, float, bool or None, not datetime
By the way, this is an example, the original dictionary really needs to be indexed by the datetime. If it's really impossible I will consider using timestamp, but I prefer to avoid because it is unreadable