I have the following JSON document being sent from an SNS HTTPs subscription to an API Gateway endpoint (backed by a Python2.7 Lambda function). In this case, I have a Cloudwatch Alarm that's configured to send to SNS, which then sends to this Gateway endpoint. SNS packages that alarm (which is JSON) into the "body" field of the message it sends to Gateway.
I need to extract the JSON document out of the "body" field, but by this point it's been properly mangled with escape characters and new lines and json.loads() is not liking it at all.
How can I read the value of "body" back into a JSON document using Python2.7 within a Lambda function? I've tried cleaning it by removing '\n' and '\', but I'm just striking out!
Here is the JSON as received by Lambda:
{
"body": "{\n \"Type\" : \"Notification\",\n \"MessageId\" : \"944c9xxx3-c98d636ff2c7\",\n \"TopicArn\" : \"arn:aws:sns:us-west-2:xxx6xx:sxxxr-sns-topic\",\n \"Subject\" : \"ALARM: \\\"hhh\\\" in US West (Oregon)\",\n \"Message\" : \"{\\\"AlarmName\\\":\\\"hhh\\\",\\\"AlarmDescription\\\":null,\\\"AWSAccountId\\\":\\\"8xxx\\\",\\\"NewStateValue\\\":\\\"ALARM\\\",\\\"NewStateReason\\\":\\\"Threshold Crossed: 1 out of the last 1 datapoints [0.333370380661336 (13/06/18 18:06:00)] was greater than or equal to the threshold (0.1) (minimum 1 datapoint for OK -> ALARM transition).\\\",\\\"StateChangeTime\\\":\\\"2018-06-13T18:16:56.457+0000\\\",\\\"Region\\\":\\\"US West (Oregon)\\\",\\\"OldStateValue\\\":\\\"INSUFFICIENT_DATA\\\",\\\"Trigger\\\":{\\\"MetricName\\\":\\\"CPUUtilization\\\",\\\"Namespace\\\":\\\"AWS/EC2\\\",\\\"StatisticType\\\":\\\"Statistic\\\",\\\"Statistic\\\":\\\"AVERAGE\\\",\\\"Unit\\\":null,\\\"Dimensions\\\":[{\\\"name\\\":\\\"InstanceId\\\",\\\"value\\\":\\\"i-07bxxx26\\\"}],\\\"Period\\\":300,\\\"EvaluationPeriods\\\":1,\\\"ComparisonOperator\\\":\\\"GreaterThanOrEqualToThreshold\\\",\\\"Threshold\\\":0.1,\\\"TreatMissingData\\\":\\\"\\\",\\\"EvaluateLowSampleCountPercentile\\\":\\\"\\\"}}\",\n \"Timestamp\" : \"2018-06-13T18:16:56.486Z\",\n \"SignatureVersion\" : \"1\",\n \"Signature\" : \"fFunXkjjxxxvF7Kmxxx\",\n \"SigningCertURL\" : \"https://sns.us-west-2.amazonaws.com/SimpleNotificationService-xxx.pem\",\n \"UnsubscribeURL\" : \"https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=axxxd\"\n}",
"resource": "/message",
"requestContext": {
"requestTime": "13/Jun/2018:18:16:56 +0000",
"protocol": "HTTP/1.1",
"resourceId": "m4sxxxq",
"apiId": "2v2cthhh",
"resourcePath": "/message",
"httpMethod": "POST",
"requestId": "f41e8-8cbd-57ad9e625d12",
"extendedRequestId": "xxx",
"path": "/stage/message",
"stage": "stage",
"requestTimeEpoch": 1528913816627,
"identity": {
"userArn": null,
"cognitoAuthenticationType": null,
"accessKey": null,
"caller": null,
"userAgent": "Amazon Simple Notification Service Agent",
"user": null,
"cognitoIdentityPoolId": null,
"cognitoIdentityId": null,
"cognitoAuthenticationProvider": null,
"sourceIp": "xxx",
"accountId": null
},
"accountId": "xxx"
},
"queryStringParameters": {
"id": "CBxxx69"
},
"httpMethod": "POST",
"pathParameters": null,
"headers": {
"Content-Type": "text/plain; charset=UTF-8",
"Via": "1.1 xxx.cloudfront.net (CloudFront)",
"Accept-Encoding": "gzip,deflate",
"CloudFront-Is-SmartTV-Viewer": "false",
"x-amz-sns-subscription-arn": "arn:aws:sns:us-west-2:xxx:sxxx-nxxx-sns-topic:xxx",
"CloudFront-Forwarded-Proto": "https",
"X-Forwarded-For": "54.240.xxx, 54.182.xxx",
"CloudFront-Viewer-Country": "US",
"User-Agent": "Amazon Simple Notification Service Agent",
"X-Amzn-Trace-Id": "Root=1-5b21xxx53acea6642317ed4",
"x-amz-sns-topic-arn": "arn:aws:sns:us-west-2:xxxx:sxxxier-sns-topic",
"Host": "2vxxx.execute-api.us-west-2.amazonaws.com",
"X-Forwarded-Proto": "https",
"X-Amz-Cf-Id": "xxx",
"CloudFront-Is-Tablet-Viewer": "false",
"X-Forwarded-Port": "443",
"x-amz-sns-message-type": "Notification",
"CloudFront-Is-Mobile-Viewer": "false",
"x-amz-sns-message-id": "xxx",
"CloudFront-Is-Desktop-Viewer": "true"
},
"stageVariables": null,
"path": "/message",
"isBase64Encoded": false
}