如何将此JSON字符串格式化为JSON对象?

时间:2018-06-13 21:17:34

标签: python json aws-lambda aws-api-gateway

我将以下JSON文档从SNS HTTPs订阅发送到API网关端点(由Python2.7 Lambda函数支持)。在这种情况下,我有一个Cloudwatch警报,它被配置为发送到SNS,然后SNS发送到此网关端点。 SNS打包报警(即JSON)进入"机构"它发送给Gateway的消息字段。

我需要从" body"中提取JSON文档。字段,但到目前为止,它已经被转义字符和新行正确地修改了,json.loads()根本不喜欢它。

如何读取" body"的值?在Lambda函数中使用Python2.7返回JSON文档?我已经尝试通过删除' \ n'来清理它。和' \',但我只是罢了!

以下是Lambda收到的JSON:

{
"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
}

1 个答案:

答案 0 :(得分:1)

如果我将您的粘贴样本用作原始字符串,则效果很好:

>>> j = r'''...your sample pasted here...'''
>>> data = json.loads(j)
>>> bodydata = json.loads(data['body'])
>>> bodydata['Type']
u'Notification'

似乎,你上面粘贴的是repr表单,用Python打印出来