Skip to content

Instantly share code, notes, and snippets.

@PabTorre
Created September 18, 2017 15:27
Show Gist options
  • Save PabTorre/4cf81a4d8c13bbab57b102227d85f081 to your computer and use it in GitHub Desktop.
Save PabTorre/4cf81a4d8c13bbab57b102227d85f081 to your computer and use it in GitHub Desktop.
Cleans JSON object to make it DynamoDB friendly.
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