- Newest
- Most votes
- Most comments
Not out of the woods yet: Here's the code now:
import json import boto3 from boto3.dynamodb.conditions import Key
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('D1-Sites')
response = table.get_item(Key={'SitesID': '1'})
print ('response: ')
print(response)
# from re:Post
item = response.get('D1-Website', {'SitesID': '1'}) # Provide a default value to handle
d1_website = item.get('D1-Website')
print('d1_website is:')
print(d1_website)#print the URL
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
And the response: response: {'Item': {'D1-Pricing': 'https://pricingdayones.godaddysites.com/', 'D1-QnA': 'https://faqdayoneslive.godaddysites.com/', 'SitesID': '1', 'D1-Youtube': 'https://m.youtube.com/@DayOnes_live', 'D1-ArtistQuickGuide': 'artistguide.dayones.live', 'D1-FanQuickGuide': 'fanguide.dayones.live', 'D1-Website': 'http://dayones.live'},
d1_website is: None
Printing the response from "table.get_item(Key={'SitesID': '1'})" shows indeed a list of the table variables for SitesID = 1....the Partition Key. "D1-Website" is the last return from the attributes in the table with the value.
However, I am still getting a none in variable d1_website attempting ti get the data. What have I not done correctly?
Peter
Hello,
response.get('D1-Website') tries to retrieve the value of the 'D1-Website' key directly from the response dictionary. However, since the response dictionary doesn't have a top-level key named 'D1-Website', this operation returns None. Instead, you should first extract the item from the response and then access the 'D1-Website' attribute from the item
response = table.get_item(Key={'SitesID': '1'}) item = response.get('Item', {}) # Provide a default value to handle None response d1_website = item.get('D1-Website')
As reported here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.ReadItem.html
you may want to retrieve the item via response["Item"]
first, and then by the field.
Relevant content
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated a year ago
Is none because you have to get the “Item” field: response[“Item”][“D1-Website”]