Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
Vikash Avatar asked Dec 29 '25 09:12

Vikash


1 Answers

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'}
                  })
like image 162
F_SO_K Avatar answered Jan 01 '26 00:01

F_SO_K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!