I have a string representation of a JSON object.
dumped_dict = '{"debug": false, "created_at": "2020-08-09T11:24:20"}'
When I call json.loads with this object;
json.loads(dumped_dict)
I get;
{'created_at': '2020-08-09T11:24:20', 'debug': False}
There is nothing wrong in here. However, I want to know if there is a way to convert the above object with json.loads to something like this:
{'created_at': datetime.datetime(2020, 08, 09, 11, 24, 20), 'debug': False}
Shortly, are we able to convert datetime strings to actual datetime.datetime objects while
calling json.loads?
My solution so far:
For further reference on the use of object_hook: JSON encoder and decoder
In my case the json string is coming from a GET request to my REST API. This solution allows me to ‘get the date right’ transparently, without forcing clients and users into hardcoding prefixes like
__date__into the JSON, as long as the input string conforms to DATE_FORMAT which is:The regex pattern should probably be further refined
PS: in case you are wondering, the json_string is a MongoDB/PyMongo query.