I am having below pydantic models.
class SubModel(BaseModel):
columns: Mapping
key: List[str]
required: Optional[List[str]]
class Config:
anystr_strip_whitespace: True
extra: Extra.allow
allow_population_by_field_name: True
class MyModel(BaseModel):
name: str
config1: Optional[SubModel]
config2: Optional[Mapping]
class Config:
anystr_strip_whitespace: True
extra: Extra.allow
allow_population_by_field_name: True
When I am trying to do a dumps
on this, I am getting model is not JSON serializable
from io import BytesIO
from orjson import dumps
bucket = s3.Bucket(bucket_name)
bucket.upload(BytesIO(dumps(data)), key, ExtraArgs={'ContentType': 'application/json'})
Error -
TypeError: Type is not JSON serializable: MyModel
data
is a normal python dictionary with one of item of type MyModel
. Tried to use .json()
but get dict has no attribute json
.
I am stuck here. Can someone help me.
Got similar issue for FastAPI
response, solved by:
return JSONResponse(content=jsonable_encoder(item), status_code=200)
or can be just like this:
return jsonable_encoder(item)
where jsonable_encoder
is:
from fastapi.encoders import jsonable_encoder
More details are here: https://fastapi.tiangolo.com/tutorial/encoder/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With