freecode111
freecode111

Reputation: 93

How do I ONLY return the value from my DynamoDB table?

I'm trying to return a value from an attribute in my dynamodb table. The code for the REST API is below. The return value I currently get is {"N": "101"}. How Do I get the return value to be 101? Before I added in count = response['Attributes']['Visits'] the return value also included the metadata.

import boto3
import json

dynamodb = boto3.client('dynamodb')
def lambda_handler(event, context):
    response = dynamodb.update_item(
        TableName='ResumeCounter',
        Key={
            'Site': {
                'N': '0'
            }
        },
        UpdateExpression='SET Visits = Visits + :inc',
        ExpressionAttributeValues={
            ':inc': {'N': '1'}
        },
        ReturnValues="UPDATED_NEW"
    )
    res = dynamodb.get_item(
        TableName='ResumeCounter',
        Key={
            'Site': {
                'N': '0'
            }
        },
        ProjectionExpression='Visits',
    )
    count = response['Attributes']['Visits']
    return count  


Upvotes: 0

Views: 1906

Answers (1)

Shubham Jain
Shubham Jain

Reputation: 5526

To get the output from the update response object use

count = response['Attributes']['Visits']['N']
return count  

and to get from get_Item use

count = response['Item']['Visits']['N']

Upvotes: 2

Related Questions