9

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?

2
  • Can you provide the code where you use the put_item? Looks like you are adding the types yourself. Commented Jul 31, 2017 at 17:32
  • Actually, it looks like it has to do with the fact that I'm using 'client' objects instead of 'resource' objects. Resources are a more abstract version that helps to take care of the object types. Commented Jul 31, 2017 at 18:21

1 Answer 1

11
+350

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:

Sign up to request clarification or add additional context in comments.

1 Comment

I tried using boto3.resource(), still it returns along with datatypes. What could be the issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.