14

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.

Naxi
  • 1,504
  • 5
  • 33
  • 72

3 Answers3

11

Got similar isse 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/

Dharman
  • 30,962
  • 25
  • 85
  • 135
Taraskin
  • 561
  • 9
  • 15
2

What about just setting a default encoder (which is used if all else fails)?

orjson.dumps(
    MyModel(name="asd"),
    default=lambda x: x.dict()
)
# Output: b'{"name":"asd","config1":null,"config2":null}'

Or with more nested:

orjson.dumps(
    {"a_key": MyModel(name="asd")}, 
    default=lambda x: x.dict()
)
# Output: b'{"a_key":{"name":"asd","config1":null,"config2":null}}'

If you have other types than Pydantic, just make a function and handle all types you have separately.

It also works with json library from standard library (for those that don't use orjson).

miksus
  • 2,426
  • 1
  • 18
  • 34
1

Here the problem is that pydantic models are not json serializable by default, in your case, you can call data.dict() to serialize a dict version of your model.

from io import BytesIO
from orjson import dumps

bucket = s3.Bucket(bucket_name)
bucket.upload(BytesIO(dumps(data.dict())), key, ExtraArgs={'ContentType': 'application/json'})
Josep Pascual
  • 168
  • 1
  • 7