How to update/create nested object in dynamo

Viewed 85

I have few items in DynamoDB like this.

Example 1 -

{
    "policyName":"Some Name",
    "domainBypassList":{
        "custom":[],
        "default":[]
    }
    "applicationBypassList":{
        "default":{
            "Mac":[],
            "Windows":[]
        },
        "custom":{
            "Mac":[],
            "Window":[]
        }
}

Example 2 -

{
    "policyName":"Some Name",
    "domainBypassList":{
        "default":[]
    }
    "applicationBypassList":{
        "default":{
            "Mac":[],
            "Windows":[]
        }
}

I want to add/update the custom of domainBypassList & applicationBypassList. You can see in Example 1 the custom attribute exist in both domainBypassList & applicationBypassList. However, in Example 2 you can see that custom attribute is missing. So if the custom attribute is missing then it should be added or if it exist then it should be updated.

I wrote a simple query which works only if the custom attribute exist in both domainBypassList & applicationBypassList i.e Example 1.

res = self.service_context.policies.update_item(
            Key={
                'tenantId': self.event["user"]['tenantId'],
                'policyName': self.policy_name
            },
            UpdateExpression="set domainBypassList.custom=:dbl, applicationBypassList.custom=:abl, updatedAt=:uat",
            ExpressionAttributeValues={
                ':abl': self.payload["applicationBypassList"],
                ':dbl': self.payload["domainBypassList"],
                ':uat': datetime.datetime.now().strftime("%Y/%m/%d, %I:%M %p")
            },
            ReturnValues="UPDATED_NEW"
        )

How can I make this query make work with both the cases.

Here is the object that I have for custom of both domainBypassList & applicationBypassList.

{
    "applicationBypassList":{
        "Mac": [
        "Zoom.us",
        "fool.is"
    ],
    "Windows": [
        "Zoom.exe",
        "cool.com"
    ]
    },
    "domainBypassList":["some.com"]
}
0 Answers
Related