[Python] parse json file to Pydantic model

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    I created a model of Pydantic. But it does not convert and outputs an error Please tell me,what is wrong.

    classDTO:

    from pydantic import BaseModel,Field
    from typing import List,Dict
    from datetime import date

    class OurBaseModel(BaseModel):
    pass
    #class Config:
    #orm_mode = True

    class SessionSubjectDTO(OurBaseModel):
    edu_year: int
    semester_type: str

    class MarkDTO(OurBaseModel):
    semester_number: int
    subject_name: str
    control_type: str
    mark: str # or int
    session_subject: SessionSubjectDTO #= Field(None, alias="SessionSubjectDTO")

    class MarksDTO(OurBaseModel):
    __root__: List[MarkDTO]

    class AttestationDTO(BaseModel):
    subject_name: str
    value: int
    attestation_start_date: date

    class AttestationsDTO(OurBaseModel):
    __root__: List[AttestationDTO]

    class DebtDTO(OurBaseModel):
    semester_number: int
    subject_name: str
    control_type: str
    session_subject: SessionSubjectDTO #= Field(None, alias="SessionSubjectDTO")

    class DebtsDTO(OurBaseModel):
    __root__: List[DebtDTO]

    class SkipDTO(OurBaseModel):
    valid: int
    no_valid: int
    attestation_start_date: date

    class SkipsDTO(OurBaseModel):
    __root__: List[SkipDTO]

    class StudentDTO(OurBaseModel):
    uid: str
    marks: MarksDTO
    attestations: AttestationsDTO
    debts: DebtsDTO
    skips: SkipsDTO

    class StudentsDTO(OurBaseModel):
    __root__: List[StudentDTO]


    example.json:

    [
    {
    "uid": "61c689ac-98a1-11e9-8198-4ccc6a2d123b",
    "marks": [
    {
    "semester_number": 1,
    "subject_name": "454",
    "control_type": "5",
    "mark": "3.",
    "date": "2019-12-27",
    "session_subject": {
    "id": 4228,
    "edu_year": 2019,
    "semester_type": "1"
    }
    }
    ],
    "attestations": [
    {
    "subject_name": "133",
    "value": 2,
    "attestation_start_date": "2019-10-07",
    "attestation_end_date": "2019-10-12"
    }
    ],
    "debts": [
    {
    "semester_number": 4,
    "subject_name": "323",
    "control_type": "12",
    "session_subject": {
    "id": 22856,
    "edu_year": 2020,
    "semester_type": "20"
    }
    }
    ],
    "skips": [
    {
    "valid": null,
    "no_valid": null,
    "attestation_start_date": "2020-03-09",
    "attestation_end_date": "2020-03-14"
    }
    ]
    }
    ]


    main.py:

    students = pydantic.parse_file_as(path='192.json', type_=classDTO.StudentsDTO)


    Errors:

    Traceback (most recent call last):
    File "main.py", line 73, in <module>
    students = pydantic.parse_file_as(path='192.json', type_=classDTO.StudentsDTO)
    File "pydantic\tools.py", line 60, in pydantic.tools.parse_file_as
    File "pydantic\tools.py", line 38, in pydantic.tools.parse_obj_as
    File "pydantic\main.py", line 331, in pydantic.main.BaseModel.__init__
    pydantic.error_wrappers.ValidationError: 424 validation errors for ParsingModel[StudentsDTO]
    __root__ -> __root__ -> 0 -> attestations -> __root__ -> 18 -> value
    none is not an allowed value (type=type_error.none.not_allowed)
    __root__ -> __root__ -> 0 -> attestations -> __root__ -> 19 -> value
    none is not an allowed value (type=type_error.none.not_allowed)
    __root__ -> __root__ -> 0 -> attestations -> __root__ -> 20 -> value
    none is not an allowed value (type=type_error.none.not_allowed)
    ...
    __root__ -> __root__ -> 16 -> skips -> __root__ -> 1 -> no_valid
    none is not an allowed value (type=type_error.none.not_allowed)


    I tried to solve the problem with Custom Root Types:


    Pydantic models can be defined with a custom root type by declaring the field. __root__

    The root type can be any type supported by pydantic, and is specified by the type hint on the __root__ field. The root value can be passed to the model __init__ via the __root__ keyword argument, or as the first and only argument to parse_obj.
    ​

    Continue reading...

Compartilhe esta Página