Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

524
Visualizações
How to map values from nested dictionary and list using Pydantic library?

I am trying to map values from a nested dict to Pydantic model. It doesnt work, because dict has a nested structure.

Now I have such format of the response:

{
    "data": {
        "3": {  <---- its id
            "type": "Polygon",   <---- its coords
            "coordinates": []},
         "4": { 
            "type": "Polygon",  
            "coordinates": []}}}

I am trying to do response with such structure:

{"data": 
    {
    "1": [{
      "_id": 3,
      "coords": {
        "type": "Polygon",
        "coordinates": []}}],
     "2": [{
          "id": 4,
           "coords": {
            "type": "Polygon",
            "coordinates": []}]
 },....}

Keys "1", "2" and so on are generating in the loop using func enumerate()

I have a Pydantic model with attributes:

class Serializer(BaseModel):

    id: Optional[int] = None
    coords: Optional[dict] = None

But have validation Error

pydantic.error_wrappers.ValidationError: 2 validation errors 
response -> cluster_id
  field required (type=value_error.missing)
response -> coords_cluster
  field required (type=value_error.missing)

How can I map the value from dict using Pydantic model?

Is there an easy way instead of using a root_validator to parse the given structure to my flat pydantic Model?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda