Last active
June 30, 2021 11:49
-
-
Save JamieCressey/94969418492f5a05b188e63954a23386 to your computer and use it in GitHub Desktop.
Coverts a Python Boto3 DynamoDB item to a standard dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_dynamo_item(item): | |
resp = {} | |
if type(item) is str: | |
return item | |
for key,struct in item.iteritems(): | |
if type(struct) is str: | |
if key == 'I': | |
return int(struct) | |
else: | |
return struct | |
else: | |
for k,v in struct.iteritems(): | |
if k == 'L': | |
value = [] | |
for i in v: | |
value.append(parse_dynamo_item(i)) | |
elif k == 'S': | |
value = str(v) | |
elif k == 'I': | |
value = int(v) | |
elif k == 'M': | |
value = {} | |
for a,b in v.iteritems(): | |
value[a] = parse_dynamo_item(b) | |
else: | |
key = k | |
value = parse_dynamo_item(v) | |
resp[key] = value | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this @JamieCressey, note if you are in python 3 use dict.items() instead of dict.iteritems()
https://stackoverflow.com/questions/30418481/error-dict-object-has-no-attribute-iteritems-when-trying-to-use-networkx