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?

4
  • 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. Commented Oct 20, 2021 at 10:54
  • @brennanyoung Hi thanks for the input, does this clear things up? Commented Oct 20, 2021 at 12:45
  • You can just annotate this field as Dict or try to use solution from here Commented Oct 20, 2021 at 15:36
  • @SnowBoundShip53 well the first sentence is still lacking a meaningful ending after the word "specific". Commented Oct 22, 2021 at 12:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.