Pydantic to load list of nested json objects thows missing value

21 Views Asked by At

I have the following pydantic objects:

class NestedObject(BaseModel):
    id: int

class MainObject(BaseModel):
    id: int
    nested_objects: list[NestedObject]

Also I have a JSON string that I want pydantic to build the objects from:

bla = """
{
    "id": 1,
    "nested_objects:": [
        {
            "id": 1
        },
        {
            "id": 2
        }
    ]
}
"""

How I load my json string:

MainObject.model_validate_json(bla)

When I try to load the JSON string with the model, it gives me a ValidationError saying that nested_objects are missing.

I have been scratching my head over this, since I couldn't find anything in the documentation stating that it is not possible to validate from complex nested json structures.

Do I need a field_validator in order to make this work, or have I missed something?

0

There are 0 best solutions below