Search code examples
jsonamazon-web-servicesamazon-dynamodbboto3amazon-dynamodb-streams

boto3 dynamodb pass json as parameter


So the following code works

import boto3

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.Table('mytest')

myid = "123"
mylocation = "paris"

response = table.put_item(
   Item={
        'myid': myid,
        'mylocation': mylocation

    }
)

print("PutItem succeeded:")

But how do i pass json at runtime instead of hardcoding the myid and mylocation. I want to pass something like "{'myid':'123', 'mylocation:'paris'}" as a json string to put_item, Items. Is that possible in boto3 for dynamodb?


Solution

  • It is possible for flat JSON structure or Scalar DynamoDB data types (String, Number and Boolean). However, you may need to explore more for all different types of DynamoDB data types (such as Map, List, Set etc).

    yearkey - Stored as Number in DynamoDB

    Title - Stored as String in DynamoDB

    jsonString = {'title' : "The Big New Movie", 'yearkey' : 1500};        
    
    response = table.put_item(
       Item=jsonString
    )