chatbotAPI / src /helpers /json_convertor.py
dhruv4023's picture
Synced repo using 'sync_with_huggingface' Github Action
a3386d3 verified
raw
history blame contribute delete
430 Bytes
import json
from bson import ObjectId
from datetime import datetime
def custom_serializer(obj):
if isinstance(obj, ObjectId):
return str(obj)
if isinstance(obj, datetime):
return obj.isoformat() # Convert datetime to ISO 8601 format
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
def convert_to_json(data):
return json.dumps(data, default=custom_serializer, indent=4)