简体   繁体   中英

How do I parse out value from AWS Lambda python response?

Im trying to get the "counter_value" number to be passed into my API gateway. This is what is currently passed in when I return response from my lambda function

{"Attributes": {"counter_value": {"N": "89"}}, "ResponseMetadata": {"RequestId": "OOQNNHDS4S5E2IPHHA6PHVGS0JVV4KQNSO5AEMVJF66Q9ASUAAJG", "HTTPStatusCode": 200, "HTTPHeaders": {"server": "Server", "date": "Tue, 21 Jul 2020 01:58:26 GMT", "content-type": "application/x-amz-json-1.0", "content-length": "43", "connection": "keep-alive", "x-amzn-requestid": "OOQNNHDS4S5E2IPHHA6PHVGS0JVV4KQNSO5AEMVJF66Q9ASUAAJG", "x-amz-crc32": "897850636"}, "RetryAttempts": 0}}

Here is my Lambda Function(Python)

import boto3
def lambda_handler(event, context):
    dynamodb = boto3.client('dynamodb')
    
    response = dynamodb.update_item(
        TableName = 'Ordeproject',
        Key = {
            'id': {'S':'counter'}
        },
        UpdateExpression = 'SET counter_value = counter_value + :add',
        ExpressionAttributeValues = {
            ':add': {'N':'1'}
        },
        ReturnValues = "UPDATED_NEW"
    )
    print("UPDATING ITEM")
    print(response)
    return response

Maybe I misunderstand the issue, but to get the counter_value from response you can do the following:

counter_value = response['Attributes']['counter_value']['N']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM