0

I'm receiving following data from DynamoDB as a response to client scan API call.

Data = [{'id': {'S': '5'},
         'FirstName': {'S': 'Prashanth'},
         'LastName': {'S': 'Wadeyar'},
         'ClientName': {'S': 'Test'}}]

I want to handle this response and get the output as

{'FirstName':'Prashanth', 'LastName': 'Wadeyar', 'ClientName': 'Test'}

I can handle it by separating it like

for field_obj in data:
    obj = (field_obj['FirstName'])

but to get value of Firstname, Key 'S' may differ for each object. like boolean string, list etc.

is there a easy way to get the key and value pairs.

3 Answers 3

2

If you don't want to bring external dependencies you can use Table class as described here.

import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('users')

response = table.get_item(
    Key={
        'username': 'janedoe',
        'last_name': 'Doe'
    }
)
item = response['Item']
print(item)

Expected Output:

{u'username': u'janedoe',
 u'first_name': u'Jane',
 u'last_name': u'Doe',
 u'account_type': u'standard_user',
 u'age': Decimal('25')}

But my personal preference everytime I hear about Python and DynamoDB is to use PynamoDB: https://pynamodb.readthedocs.io/en/latest/ which is sort of an ORM for DynamoDB.

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

Comments

0

Above answer from Mayank seems to work well, but I also wanted to map fields I want to return in a list. Hence I mapped field and then was able to get the result I was looking for

FIELD_MAP = ['firstName', 'lastName',
               'clientName']

    for field_obj in response['Items']:
        for key in FIELD_MAP:
            if field_obj.get(key):
                obj = (field_obj.get(key))
                for i in obj:
                    value = obj[i]
                db_result.append({key: value})
    print(db_result)

4 Comments

What about numbers and complex types like map/list/set?
We would need to loop through again if its a list. Although my query result does not have any other types. it would be the same
you will need recursive walk, not a loop, do you really want it? There is much simpler 1 line solution, I am genuinely wondering why would you want to reimplement something that is already implemented in boto for you.
yeah, I got it @Andrey, I was restricted to use only client and not resource method. Hence had to deal with it. Using resource object makes job lot more simpler. Thanks for your comment
-1

Something like this could work, where you don't need to care about the internal keys(S in your case):

In [1018]: d = {}

In [1016]: for i in Data: 
      ...:     for k,v in i.items():
      ...:         if k != 'id':
      ...:             if isinstance(v, dict): 
      ...:                 for j, val in v.items(): 
      ...:                     d[k] = val 
      ...:                  

In [1024]: d                                                                                                                                                                                                
Out[1024]: {'FirstName': 'Prashanth', 'LastName': 'Wadeyar', 'ClientName': 'Test'}

2 Comments

Thank you, I was able to get through with something like below as I wanted to iterate each item and append through the list for field_obj in data: for key in FIELD_MAP: if field_obj.get(key): obj = (field_obj.get(key)) for i in obj: value = obj[i] db_result.append({key: value})
This is a bad approach for the following reasons: 1) it ignores types, all primitive values will come out as strings 2) it doesn't support nested structures at all (set/map/list). For those reasons I am downvoting it (and not because I have my answer).

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.