Dawny33
Dawny33

Reputation: 11081

How do I insert a map into DynamoDB table?

I have the following line of code :

table.put_item( Item={'filename' : key, 'status' : {'M' : iocheckdict }})

The iocheckdict looks like this:

{'A': 'One', 'C': 'Three', 'D': 'Four', 'B': 'Two', 'E': 'Five'}

So, when I am running the code, I get this error:

An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Type mismatch for key status expected: S actual: M

Why am I getting this, even though I mentioned M as the type of the data?

PS : I have 2 columns filename and status in my table


Attribute definitions of the table:

"AttributeDefinitions": [
    {
        "AttributeName": "filename",
        "AttributeType": "S"
    },
    {
        "AttributeName": "status",
        "AttributeType": "S"
    }
],

I understand that the type of status is S, but I haven't found the map type while creating the table. All I found are string, binary and number.

Upvotes: 6

Views: 15622

Answers (2)

omuthu
omuthu

Reputation: 6333

Easier way to insert into dynamodb

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("table_name")

item={}
item['filename'] = key
item['status'] = {'A': 'One', 'C': 'Three', 'D': 'Four', 'B': 'Two', 'E': 'Five'}

table.put_item(Item=item)

Upvotes: 9

lambfrier
lambfrier

Reputation: 837

I was also confused by the doc focusing on the data types. Thanks to @omuthu's answer, I realised that one just needs to pass in the dict object. I think a slightly more succinct answer to the question is to simplify the original put_item() call by stripping out the 'M' (map) additional level, .i.e:

table.put_item( Item={'filename':key, 'status':iocheckdict} )

or as a working example:

import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("test")

key="test1235.txt"
iocheckdict = {'A': 'One', 'C': 'Three', 'D': 'Four', 'B': 'Two', 'E': 'Five'}

table.put_item( 
    Item={
        'filename': key, 
        'status':   iocheckdict 
    }
)

Upvotes: 0

Related Questions