[Python] Pydantic convert underscores in keys to hyphen when exporting to json

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have a class

    class SampleModel(BaseModel):
    first_name: str
    last_name int


    When I export it to json, I want to json to be

    {
    "first-name": "john doe",
    "last-name": 1234
    }


    I was going to use aliases, but does pydantic have a config or an option to directly do this for all fields in a model?

    Continue reading...

Compartilhe esta Página