Reputation: 149
I am having trouble using the client.put_item method to put a list of dicts to my table. The dict is in the form: {"name": "beef", "price":5.23}
. An example of a list like this is: [{"name": "chicken", "price":6.23}, {"name": "beef", "price":5.34}, {"name": "pork", "price":8.48}]
where the list size could vary. So far I have
def create_order(order_id, cart, total):
dynamodb = boto3.client('dynamodb')
table = 'Orders'
response = dynamodb.put_item(TableName=table,
Item={
'order_id': {"S":order_id},
'order_total': {"N":str(total)},
'cart':{"L":cart}
}
)
return response
It works with order_id and order_total, but I can't figure out how to format the cart.
Upvotes: 4
Views: 8509
Reputation: 10333
Couple of things to Note:
parse_float=Decimal
.Here is an example:
import json
import boto3
from decimal import Decimal
dynamodb = boto3.resource('dynamodb',region_name='us-east-1')
table = dynamodb.Table('Orders')
order_id = "1000"
total = 100
cartBefore = [{"name": "chicken", "price":6.23}, {"name": "beef", "price":5.34}, {"name": "pork", "price":8.48}]
cart = json.loads(json.dumps(cartBefore), parse_float=Decimal)
response = table.put_item(
Item={
'id': '10',
'order_id': order_id,
'order_total': total,
'cart': cart
}
)
Upvotes: 4