I ran into a problem when writing Python datetime
objects to a JSON object. It can be solved like this:
date_handler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
for doc in docs:
temp_file.write(json.dumps(doc, default = date_handler))
The important part here is the lamba
which is producing ISO formatted strings from a datetime
object. Then the json.dumps
takes the argument default pointing at the lambda
.