How should I handle ObjectId with Flask?

I am currently storing all documents’ ids and references as ObjectId rather than string.

To send documents from my Flask app to my frontend, I have to convert all ObjectIds to string first. This is due to the error TypeError: Object of type ObjectId is not JSON serializable

When my server receives documents from my frontend, I have to convert these strings back to ObjectIds before performing an update operation on the document.

I am contemplating storing all id as strings in order to avoid performing these two additional steps. However, I understand that ObjectId is more efficient which is why I have been resisting this change.

Would really appreciate some advice on how should I handle this situation.

This is usually how I handle it,

from bson import json_util, ObjectId
import json

#Dump loaded BSON to valid JSON string and reload it as dict
page_sanitized = json.loads(json_util.dumps(page))
return page_sanitized

json_util.dumps(page) would convert _id: Object(...) to _id: {"$oid": hex_representation} which makes it unintuitive for the frontend