Skip to content

Instantly share code, notes, and snippets.

@gbhorwood
Last active May 6, 2019 21:35
Show Gist options
  • Save gbhorwood/cfa291ec1be5786f10038bbe7959ecc7 to your computer and use it in GitHub Desktop.
Save gbhorwood/cfa291ec1be5786f10038bbe7959ecc7 to your computer and use it in GitHub Desktop.
AWS lambda munge response from boto3 rds-data execute_sql
def mungeBoto3Rds(response):
# get a list of all the rows of data returned
records = response.get('sqlStatementResults')[0].get('resultFrame').get('records')
# get a list of keys so that element 0 of keys is the name of the column for
# the data in element 0 of records
keysList = list(map(lambda i: str(i.get('name')), response.get('sqlStatementResults')[0].get('resultFrame').get('resultSetMetadata').get('columnMetadata')))
# an empty list to hold our newly-munged data
data = []
for record in records:
# a dict to hold our key/value pairs for one record
tmp = {}
# for each element in this record, ie each column in this row
for i in range(0, len(record.get('values')) ):
# the values are keyed by the data type, ie 'stringValue'.
# we use this to determine if we have a json string or number
dataType = list(record.get('values')[i])[0]
dataValue = None
# cast the value according to its dataType
if dataType == "stringValue" or dataType == "blobValue":
dataValue = str(record.get('values')[i][dataType])
if dataType == "intValue" or dataType == "bigIntValue":
dataValue = int(record.get('values')[i][dataType])
if dataType == "bitValue" or dataType == "isNull":
dataValue = bool(record.get('values')[i][dataType])
if dataType == "doubleValue" or dataType == "realValue":
dataValue = float(record.get('values')[i][dataType])
# key our value by the key name found in keysList.
# element 0 of keysList is the key for element 0 of the
# data (represented by i in the loop) and so on
tmp[keysList[i]] = dataValue
data.append(tmp)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment