Attribute types tell DynamoDB what kind of data you are storing. This helps DynamoDB organize and find your data easily.
0
0
Attribute types (S, N, B, BOOL, L, M) in DynamoDB
Introduction
When you want to store text like names or descriptions.
When you need to save numbers like prices or ages.
When you want to keep binary data like images or files.
When you want to store true or false values.
When you want to save a list of items.
When you want to store a group of key-value pairs (like a small object).
Syntax
DynamoDB
AttributeName: { S: "string" }
AttributeName: { N: "number_as_string" }
AttributeName: { B: binary_data }
AttributeName: { BOOL: true_or_false }
AttributeName: { L: [list_of_attributes] }
AttributeName: { M: { map_of_attributes } }S means String, N means Number, B means Binary, BOOL means Boolean (true/false), L means List, and M means Map (like an object).
Numbers are stored as strings to keep precision.
Examples
This stores a text value "Alice" in the attribute Name.
DynamoDB
Name: { S: "Alice" }This stores the number 30 in the attribute Age.
DynamoDB
Age: { N: "30" }This stores a true/false value indicating if something is active.
DynamoDB
IsActive: { BOOL: true }This stores a list of strings "red" and "blue" in the attribute Tags.
DynamoDB
Tags: { L: [ { S: "red" }, { S: "blue" } ] }This stores a map (like an object) with City and Zip inside Address.
DynamoDB
Address: { M: { City: { S: "NY" }, Zip: { N: "10001" } } }Sample Program
This example shows a full item with different attribute types: string, number, boolean, list, and map.
DynamoDB
PUT Item {
"UserId": { S: "user123" },
"Age": { N: "25" },
"IsMember": { BOOL: true },
"Favorites": { L: [ { S: "chocolate" }, { S: "vanilla" } ] },
"Profile": { M: { "Height": { N: "170" }, "Weight": { N: "65" } } }
}OutputSuccess
Important Notes
Always wrap numbers as strings in the N type to avoid errors.
Lists (L) can hold mixed attribute types, like strings and numbers together.
Maps (M) let you nest attributes inside one another, like a small object.
Summary
Attribute types tell DynamoDB what kind of data you store.
S is for text, N is for numbers, B is for binary data, BOOL is for true/false.
L is for lists, and M is for maps (objects).