Generally, using of
json.dumps
with a custom converter.
The json.dumps method can accept an optional parameter called default which is expected to be a function. Every time JSON tries to convert a value it does not know how to convert it will call the function we passed to it. The function will receive the object in question, and it is expected to return the JSON representation of the object
Example:
import json
import datetime
d = {
'name' : 'Foo'
}
print(json.dumps(d))
d['date'] = datetime.datetime.now()
def myconverter(o):
if isinstance(o, datetime.datetime):
return o.__str__()
print(json.dumps(d, default = myconverter))
In your case, you can try a quick way like:
excel = json.dumps(final, default=str)
By default, it converts everything it doesn't know to a string.
More reference to look:
Python Serialize Datetime into JSON[
^]