skip to Main Content

dynamodb yields an item in a string format:

cdb = boto3.client('dynamodb', region_name='us-west-2')
db = boto3.resource('dynamodb', region_name='us-west-2')
table = db.Table('my-table')
response = table.scan()
my_data = response['Items']
foo = my_data[0]
print(foo)

#  {'theID': 'fffff8f-dfsadfasfds-fsdsaf', 'theNumber': Decimal('1')}

Now, when I treat this like a black-box unit, do nothing, and return it to the db via put-item, I’ll get many errors indicating none of the values in the dictionary are the expected type:

cdb.put_item(TableName='my-table', Item=foo, ReturnValues="ALL_OLD")

# many errors

I’d like to rely on boto3 to do everything and avoid manipulating the values if possible. Is there a utility available that will convert a response item into the format it needs to be to be placed back in the db?

2

Answers


  1. Chosen as BEST ANSWER

    Apparently there is also a serializer that can be used:

    from boto3.dynamodb.types import TypeSerializer
    

  2. You should use your table resource to write items because you also use it to read.

    Something like this should do the trick:

    table.put_item(Item=foo, ReturnValues="ALL_OLD")
    

    You’re reading the data using the higher-level resource API, which maps many native Python Types to DynamoDB types, and trying to write using the lower-level client API, which doesn’t do that.

    The simple solution is also to use the resource-API to write, and it will perform the mappings.

    (I’m inferring this based on your method signature for put_item, the question is not overly clear…)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search