3

I want to store a large JSON (dict) from Python in dynamoDB.

After some investigation it seems that zlib is the way to go to get compression at a good level. Using below Im able to encode the dict.

ranking_compressed = zlib.compress(simplejson.dumps(response["Item"]["ranking"]).encode('utf-8'))

The (string?) then looks like this: b'x\x9c\xc5Z\xdfo\xd3....

I can directly decompress this and get the dict back with:

ranking_decompressed = simplejson.loads(str(zlib.decompress(ranking_compressed).decode('utf-8')))

All good so far. However, when putting this in dynamoDB and then reading it back using the same decompress code as above. The (string?) now looks like this:

Binary(b'x\x9c\xc5Z\xdf...

The error I get is:

 bytes-like object is required, not 'Binary'

Ive tried accessing the Binary with e.g. .data but I cant reach it.

Any help is appreciated.

2
  • 1
    Try if you can access the content of the Binary obnect with .value. Commented Jan 31, 2017 at 0:43
  • @KlausD. Thanks! That did the trick :). Write an answer and Ill accept it! Commented Jan 31, 2017 at 20:38

1 Answer 1

2

Boto3 Binary objects have a value property.

# in general...
binary_obj.value
# for your specific case...
ranking_decompressed = simplejson.loads(str(zlib.decompress(response["Item"]["ranking_compressed"].value).decode('utf-8')))

Oddly, this seems to be documented nowhere except the source code for the Binary class here

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

Comments

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.