Get value only when calling get_item in Lambda Python

0

I am successfully getting an item from DynamoDB in a Lambda Pyton with Boto3. However, I am getting attribute: value. not just the value. Is there a way to invoke get_tem to get just the value?

Python Code: import json import boto3

def lambda_handler(event, context): dynamodb = boto3.resource('dynamodb', region_name = 'us-east-1') table = dynamodb.Table('D12Sites')

response = table.get_item(
    Key={'SitesID': '1'},
    ProjectionExpression='D12Website',
    )
resp1 = response["Item"]
print(resp1)

#resp1 is: {'D12Website': 'http://site.live'} #Need just: 'http://site.live'

return {
    'statusCode': 200,
    'body': json.dumps(response["Item"]),
}

#{'D12Website': 'http://site.live'}

profile picture
Petrus
질문됨 2달 전305회 조회
1개 답변
4

Are you Expecting the value and ignore key(attribute)(Key: Value)?

Extract the value from the response

website_value = response.get("Item", {}).get("D12Website", None)

print(website_value)
답변함 2달 전
profile picture
전문가
검토됨 2달 전
  • Just to be sure.....First, thanks. Second: this works on the variable response, not call the database table again. Correct?

  • Yes, The Response Variable Holds the result of the get_item operation which already retrieves the item from dynamodb table, so by extracting value from the response variable you are not making another call to database

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠