Vikash
Vikash

Reputation: 887

Insert Number into DynamoDB table using Boto3

My Problem:

I am trying to insert a Number attribute through boto3 Python. Which is not getting inserted into the DynamoDB table.

What I tried:

Data:

{
   "TTLTimeStamp": 1616648865, #Integer
   "Seconds": 45 , #Integer
   "CreatedDateTimeUTC":"2021-03-25T10:36:45"
}

Code:

dynamo_db_client = boto3.resource('dynamodb')

table = dynamo_db_client.Table("table_name")

with table.batch_writer() as batch:
    for row in row_list:
        try:
            batch.put_item(Item=row) #Data Object as above
        except Exception as ex:
            raise ex

Unable to insert the above data if it contains Number type.

Kindly help me to get rid off this issue.

Upvotes: 3

Views: 4006

Answers (1)

F_SO_K
F_SO_K

Reputation: 14869

When you inset data into DynamoDB you have to specify the type of data in the json object.

{
   "TTLTimeStamp": {"N": "1616648865"},
   "Seconds": {"N": "45"},
   "CreatedDateTimeUTC": {"S": "2021-03-25T10:36:45"}
}

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

Put example:

dynamodb.put_item(TableName='fruitSalad', 
                  Item={
                  'fruitName':{'S':'Banana'},
                  'key2':{'N':'value2'}
                  })

Upvotes: 4

Related Questions