Created
September 18, 2017 15:27
-
-
Save PabTorre/4cf81a4d8c13bbab57b102227d85f081 to your computer and use it in GitHub Desktop.
Cleans JSON object to make it DynamoDB friendly.
This file contains 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 clean_json(x): | |
""" | |
Cleans JSON object to make it DynamoDB friendly. | |
It removes any keys that have an empty value. | |
It moves all float values to string. | |
""" | |
out = dict() | |
if type(x) is dict: | |
for k in x: | |
if x[k] not in ["", {}, []]: | |
out[k] = clean_json(x[k]) | |
elif type(x) is list: | |
return[clean_json(x[a]) for a in range(len(x))] | |
elif type(x) is float: | |
return str(x) | |
else: | |
return x | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment