Python Dataclass (Marshmallow) : How to Handle Dynamic Response Keys

Viewed 122

I am creating dataclass Objects for Response I get. It worked fine until, I received static key'ed Response. But not sure how to handle when the keys are dynamic. For example, below is a dataclass

@dataclass_json
@dataclass
class CollectorDetails:
    items: List[Items]
    total: int = None
    isMin: bool = False
    searchId: int = None

    @property
    def total_items(self):
        return self.total

    @property
    def is_min(self):
        return self.isMin

    @property
    def search_id(self):
        return self.searchId

    @property
    def get_items(self):
        return self.items

above is easy to parse, as items, total, isMin, searchId in the response key will never change

but for below type of response where key 44897 is dynamically generated, How do I write a dataclass ?

"byId": {
            "cloudTest": {
                "44897": {
                    "id": "44897",
                    "name": "test-azure",
                    "parentGroupId": {
                        "model": "resourceGroups",
                        "id": "1"
                    },

so next time if I hit the endpoint again my response is something like below

"byId": {
            "cloudTest": {
                "55456": {
                    "id": "55456",
                    "name": "test-azure",
                    "parentGroupId": {
                        "model": "resourceGroups",
                        "id": "1"
                    },

here the response key is now '55456'

0 Answers
Related