hkjhadj1
hkjhadj1

Reputation: 886

Boto3 DynamoDB ParamValidationError When inserting nested objects

I am trying to put (insert) a nested object in my DynamoDB table. The object I have constructed as per the boto3 documentation is:

itm = {
    'uid': {
        'S': 'some-unique-id-value'
    },
    'myArray': {
        'L': [
            {
                'propOne': {
                    'S': 'this is value of prop 1'
                },
                'createdOn': {
                    'S': str(time.time())
                },
                'isActive': {
                    'BOOL': True
                },
                'propTwo': {
                    'S': 'this is value of prop 2'
                },
                'propThree': {
                    'S': 'this is value of prop 3'
                }
            }
        ]
    }
}

And then I put the item using the dynamoDB client:

dynamodb.put_item(TableName='myTableName', Item=itm)

But I am getting the exception:

botocore.exceptions.ParamValidationError: Parameter validation failed:
Unknown parameter in Item.myArray.L[0]: "propOne", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "createdOn", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "isActive", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "propTwo", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "propThree", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL

I am following this documentation:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item

My goal is to store the item as shown in this JSON object:

{
   "uid":"some-uid-val",
   "myArray":[
      {
         "propOne":"item 1 prop 1 value",
         "createdOn":"123456",
         "isActive":true,
         "propTwo":"item 1 prop 2 value",
         "propThree":"item 1 prop 3 value"
      },
      {
         "propOne":"item 2 prop 1 value",
         "createdOn":"123456",
         "isActive":true,
         "propTwo":"item 2 prop 2 value",
         "propThree":"item 2 prop 3 value"
      }
   ]
}

What am I doing wrong?

Upvotes: 3

Views: 3702

Answers (1)

Lamanus
Lamanus

Reputation: 13541

When you put an array,

'L': [
                {'... recursive ...'},
            ],

it requires recursive of an item structure. Since you have dict type of list items, you have nest dict objects in the list. Then, your item should look like as follows:

itm = {
    'uid': {
        'S': 'some-unique-id-value'
    },
    'myArray': {
        'L': [
            'M': {
                'propOne': {
                    'S': 'this is value of prop 1'
                }
            },
            'M': {
            'createdOn': {
                    'S': str(time.time())
                }
            },
            'M': {
                'isActive': {
                    'BOOL': True
                }
            },
            'M': {
                'propTwo': {
                    'S': 'this is value of prop 2'
                }
            },
            'M': {
                'propThree': {
                    'S': 'this is value of prop 3'
                }
            }
        ]
    }
}

Upvotes: 4

Related Questions