DynamoDB GetItem in Python (boto3)

get_item reads a single item by its full primary key. The boto3 resource API (Table.get_item) takes and returns native Python types, so you pass plain strings and numbers — no {"S": ...} descriptors.

Code

import boto3

dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
table = dynamodb.Table("Music")

def get_song():
    response = table.get_item(
        Key={
            "Artist": "Arturo Sandoval",
            "SongTitle": "Cubano Chant",
        },
        # Optional: return only the attributes you need
        ProjectionExpression="Artist, SongTitle, AlbumTitle, #yr",
        ExpressionAttributeNames={"#yr": "Year"},
    )

    item = response.get("Item")
    if item is None:
        print("Item not found")
        return None

    print(item)
    # {'Artist': 'Arturo Sandoval', 'SongTitle': 'Cubano Chant', 'Year': Decimal('1981'), ...}
    return item

if __name__ == "__main__":
    get_song()

Numeric attributes come back as decimal.Decimal, not float. Convert explicitly (int(item["Year"])) when you need a native number.

Explanation

  • dynamodb.Table("Music") — the resource-level handle; it auto-marshals native types both ways.
  • Key — must include every primary-key attribute: partition key only for a simple key, partition and sort key for a composite key. A partial key raises ValidationException.
  • ProjectionExpression — optional; limits which attributes return. Year is a reserved word, so it's aliased via ExpressionAttributeNames (#yr).
  • response.get("Item") — the Item key is absent when nothing matches; use .get() rather than response["Item"].
  • Reads are eventually consistent by default. Pass ConsistentRead=True for a strongly-consistent read (2× the RCU, base table only).

Do it visually

Assembling the projection or a conditional read by hand? Use the free DynamoDB Expression Builder to build the expression and copy the exact ProjectionExpression / ExpressionAttributeNames.

To browse tables and run GetItem in a GUI — key form, results grid, copy the generated boto3 code — download DynoTable.

Console 없이 DynamoDB 작업하기

DynoTable은 DynamoDB를 위한 빠른 데스크톱 클라이언트입니다 — 테이블을 탐색하고, SQL 스타일 쿼리를 실행하고, 항목을 로컬에서 편집하세요.