# TypeError: Object of type datetime is not JSON serializable

- **ID:** `python/flask-jsonify-serialization-error`
- **Domain:** python
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Returning a datetime or other non-serializable object directly from a Flask route without converting it to a string or using a custom encoder.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   Define a custom JSON encoder: class CustomEncoder(json.JSONEncoder):\n    def default(self, obj):\n        if isinstance(obj, datetime):\n            return obj.isoformat()\n        return super().default(obj)\napp.json_encoder = CustomEncoder
   ```
2. **** (90% success)
   ```
   Convert datetime to ISO format manually: return jsonify({'time': datetime.now().isoformat()})
   ```

## Dead Ends

- **** — Using str(datetime) converts to a string but loses formatting and may cause inconsistent output. (50% fail)
- **** — Setting app.json_encoder = None does not fix the issue and may break other serialization. (40% fail)
