'Unable to parse AWS DynamoDB binary get item in boto3 python

I have got the items from AWS dynamodb as dict which has a value in binary format. I am not able to retrieve the value of a binary content.

Below is the sample.

my_details  = {'route': Binary(b'gAAAABgLNW9tNcpeclIy1LSs8wKYRy9uMxgr5V4TwJmEJNZ2WVlb3Z3LtIK3PewO2SDRYkvXAh8bcZ4Ej_jBjaNi8xhU1-P2FLpcGEX2g='), 'way': '5064', '
stop': Binary(b'\xf1J\xef\xa0\xac\xb1A0\xa9\\:'), 'name': 'cfcf57'}

print(type(my_details['route']))

print(my_details['route'])
print(my_details['way'])

I need value of the route key like below

gAAAABgLNW9tNcpeclIy1LSs8wKYRy9uMxgr5V4TwJmEJNZ2WVlb3Z3LtIK3PewO2SDRYkvXAh8bcZ4Ej_jBjaNi8xhU1-P2FLpcGEX2g=

I have tried to get the value of route using mydetails['route'] but got below error

<class 'boto3.dynamodb.types.Binary'>
Traceback (most recent call last):
  File "C:/Users/Prabhakar/Documents/Projects/Test.py", line 18, in <module>
    print(my_details['route'])
    
TypeError: __str__ returned non-string (type bytes)

Please let me know how can I retrieve the binary content in python dict.



Solution 1:[1]

It is returning data with byte type, to get it in str type, you have to call .decode method on it. Here is the documentation for that: https://docs.python.org/3/library/stdtypes.html#bytes.decode

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0xc0de