0

I'm trying to parse AWS GuardDuty Json data, however some nested datafields are finding specific. Is there are way of doing something like this in pydantic:

from pydantic import Basemodel
from pydantic.main import create_model

class AwsModel(BaseModel):
  accountID: str
  service: create_model("ServiceModel")
  

and have pydantic create the service field as a dynamic model?

Clarification

I've tried this on some test data:

from pydantic import BaseModel
from pydantic.main import create_model

data = {
        "test": "test",
        "dynamic_json":{
            "dyn1": "dyn1_v",
            "dyn2": "dyn2_v",
            "dyn3": "dyn3_v"
            },
        "test2":"test2"
        }

class TestModel(BaseModel):
    test: str
    dynamic_json: create_model("dynamic_json")
    test2: str

m = TestModel.parse_obj(data)
print(m)

output: test='test' dynamic_json=dynamic_json() test2='test2'

it's created an object called dynamic_json with no fields within it, how would I get pydantic to take the nested json data and build the model from the data?

  • This seems like a good first question, just needs a word or two at the end of the first sentence to make proper sense. "finding specific..." what? Errors? Results? Please clarify. – brennanyoung Oct 20 '21 at 10:54
  • @brennanyoung Hi thanks for the input, does this clear things up? – SnowBoundShip53 Oct 20 '21 at 12:45
  • You can just annotate this field as `Dict` or try to use solution from [here](https://stackoverflow.com/questions/69216961/how-to-create-dynamic-models-using-pydantic-and-a-dict-data-type/69225701#69225701) – alex_noname Oct 20 '21 at 15:36
  • @SnowBoundShip53 well the first sentence is still lacking a meaningful ending after the word "specific". – brennanyoung Oct 22 '21 at 12:21

0 Answers0