How to work with different data types in dynamo db in python?

0

I create an item , a python dictonary and use the boto3 client to put the item created. I get an error , with code below that the paid attribute is expected as string . how can i create/update an item with a boolean attribute in python?

       dynamo = boto3.client('dynamo')
        try:
            dynamo.put_item(
                Item={
                    "dob": 1985,
                    "name": title,
                    "info": {"email": abc@xyz.com, "phone": "12093849393"},
                     "paid" : {"BOOL": False}
                }
            )
        except ClientError as err:
            logger.error(err)
asked 4 months ago197 views
3 Answers
1

In your code, you are using a mixture of data types with the low level client, which supports DynamoDB-JSON only.

Have a read of this blog post: https://aws.amazon.com/blogs/database/exploring-amazon-dynamodb-sdk-clients/

Using low level client would be:

{'mybool': {'BOOL': False}}

dynamo = boto3.client('dynamodb') #dynamodb not dynamo
try:
       dynamo.put_item(
        TableName='MyTable',
        Item={
            "dob": {"N": "1985"},
            "name": {"S": title},
            "info": {
                "M": {
                    "email": {"S": "abc@xyz.com"},
                    "phone": {"S": "12093849393"}
                }
            },
            "paid": {"BOOL": False}
        }
    )
except ClientError as err:
     logger.error(err)

High level client:

'mybool' : False

dynamo = boto3.client('dynamodb') #dynamodb not dynamo
table = dynamo.Table('MyTable')
try:
       dynamo.put_item(
        Item={
      "dob": 1985,
      "name": "title",
      "info": {
           "email": "abc@xyz.com",
           "phone": "12093849393"
       },
        "paid": False
     }
    )
except ClientError as err:
     logger.error(err)

profile pictureAWS
EXPERT
answered 4 months ago
0

Hello.

I have checked the following documents.
I confirmed that I can register it with DynamoDB by modifying the code as below in my environment.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/client/put_item.html

    dynamo.put_item(
        TableName='test',  # Please specify a TableName
        Item={
            "dob": {"N": "1985"},
            "name": {"S": title},
            "info": {
                "M": {
                    "email": {"S": "abc@xyz.com"},
                    "phone": {"S": "12093849393"}
                }
            },
            "paid": {"BOOL": False}
        }
    )
profile picture
EXPERT
answered 4 months ago
profile pictureAWS
EXPERT
reviewed 4 months ago
  • @Riku - False specified in the book value , is python right ? How does dynamo db deserialize it ?

  • I didn't have an environment to run boto3 in my local environment, so I executed it with Lambda.

    import boto3
    
    dynamo = boto3.client('dynamodb')
    title = "Test Title"
    
    def lambda_handler(event, context):
        dynamo.put_item(
            TableName='test',  # Please specify a TableName
            Item={
                "dob": {"N": "1985"},
                "name": {"S": title},
                "info": {
                    "M": {
                        "email": {"S": "abc@xyz.com"},
                        "phone": {"S": "12093849393"}
                    }
                },
                "paid": {"BOOL": False}
            }
        )
    
-1

In your code, you should not use the dictionary with "BOOL" as the key for a boolean attribute. The "BOOL" key is not required when specifying boolean values in DynamoDB.

Here's how you can update the code to create/update an item with a boolean attribute in Python:

dynamo = boto3.client('dynamo')
        try:
            dynamo.put_item(
                Item={
                    "dob": 1985,
                    "name": title,
                    "info": {"email": abc@xyz.com, "phone": "12093849393"},
                    "paid" : False
                }
            )
        except ClientError as err:
            logger.error(err)
answered 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions