Dynamodb get_item and put_item without data types in python

Viewed 2579

I currently have a python script that looks like:

import boto3
...
response = dynamodb.get_item(
  TableName = dynamodb_table_name,
  Key = {
    "snippet_id": {
      "S": snippet_id
    }
  }
)
if "Item" in response:
    item = response["Item"]
    print(json.dumps(item, indent=4, cls=DecimalEncoder))

This prints something akin to:

{
    "var_1": {
        "BOOL": false
    }, 
    "var_2": {
        "S": "Text"
    }, 
    "snippet_id": {
        "S": "3a97e45c-ffed-4c76-8bb4-b2a32f49a5d2"
    }
}

Any idea how to do the type detection and return:

{
    "var_1": False, 
    "var_2": "Text",
    "snippet_id": "3a97e45c-ffed-4c76-8bb4-b2a32f49a5d2"
}

Also, can this be done for the query as well?

1 Answers

TLDR

Use resource instead of client.

Summary

In essence, you can call boto3.client() or boto3.resource().

Client returns dynamoDB syntax, which looks like this:

'var_1' : {'S':"string"}

Resource returns normal syntax, which looks like this:

'var_1' : "string"

Further Reading

At its core, all that Boto3 does is call AWS APIs on your behalf. For the majority of the AWS services, Boto3 offers two distinct ways of accessing these abstracted APIs:

Related