I am using boto3 to query my DynamoDB table using PartiQL,
dynamodb = boto3.client(
'dynamodb',
aws_access_key_id='<aws_access_key_id>',
aws_secret_access_key='<aws_secret_access_key>',
region_name='<region_name>'
)
resp = dynamodb.execute_statement(Statement='SELECT * FROM TryDaxTable')
Now, the response you get contains a list of dictionaries that looks something like this,
{'some_data': {'S': 'XXX'},
'sort_key': {'N': '1'},
'partition_key': {'N': '7'}
}
Along with the attribute name (e.g. partition_key
), you get the data type of the value (e.g. 'N'
) and then the actual value (e.g. '7'
). It can also be seen that value does not actually come in specified data type either (e.g. partition_key
is supposed to be a number (N
), but the value is a string.
Is there some way I can get my results in a list of dictionaries without the types and also with the types applied?
That would mean something like this,
{'some_data': 'XXX',
'sort_key': 1,
'partition_key': 7
}
Notice that in addition to removing the data types, the values have also been converted to the correct type.
This is a simple record, but more complex ones can have lists and nested dictionaries. More information is available here, https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.execute_statement
Is there some way that I can get the data in the format I desire?
Or has somebody already written a function to parse the data?
I know there are several questions regarding this posted already, but most of them relate to SDKs in other languages. For instance,
AWS DynamoDB data with and/or without types?
I did not find one that has addressed this issue in Python.
Note: I want to continue to use PartiQL to query my table.
CodePudding user response:
If you can use .resource
instead of .client
you'll live at a higher abstraction layer.
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('name')
You want to use PartiQL which returns the lower-level data format, so you'll probably have to follow the advice at How to convert a boto3 Dynamo DB item to a regular dictionary in Python?