Skip to content

Instantly share code, notes, and snippets.

@smoothml
Created February 23, 2018 09:30
Show Gist options
  • Save smoothml/59fb228580746e0535df6caf5ea6892b to your computer and use it in GitHub Desktop.
Save smoothml/59fb228580746e0535df6caf5ea6892b to your computer and use it in GitHub Desktop.
2018-02-23 Serverless computing reformat_json.py
def reformat_json(response):
# rearrange the response to not be nested
# i.e. {"rates": {"EUR": 1.2}} -> {"EUR": 1.2}
rates = response.pop("rates")
response.update(rates)
# update response to DynamoDB's required format
response_updated = {}
for key, val in response.items():
# date and base fields are strings (DynamoDB has no date type) whereas everything else is a
# number
if key == "date" or key == "base":
val_type = "S"
else:
val_type = "N"
val = str(val)
response_updated[key] = {val_type: val}
return response_updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment