0

I am using Python 3.8.x and I do get the JSON Serialization error with DATETIME fields in MongoDB documents when retrieving a bunch of collections/documents in pymongo. If it is one document and I know the field, I can explicitly use a customer encoder like those mentioned at How to overcome "datetime.datetime not JSON serializable"?

But since I don't know which collections/documents have DATETIME fields, how can I apply a transformation or masking rule on all documents in a collection?

John Doe
  • 1
  • 1
  • Doesn't pymongo provide `json_util` for that? – Joe Sep 25 '20 at 02:38
  • I am trying this out for the first time and have no idea how to use json_util in pymongo. Do you think Json_util will let me set a transformation rule on all documents in a collection for datetime or other data types that cannot be serialized? – John Doe Sep 25 '20 at 13:22
  • Take a look at the examples at https://api.mongodb.com/python/current/api/bson/json_util.html. The json_util module is designed to encode/decode BSON types to and from JSON. – Joe Sep 25 '20 at 15:58
  • I got it to work for me using json_utils. Had to first dump and then loads the document again. Thanks much !. – John Doe Sep 25 '20 at 21:38

0 Answers0