title: Python - AWS Lambda extract a key from JSON input
body: <p>Im trying to implement a function that will get the event from cloudwatch and print the results. I am able to get the event but I want to extract one particular key from that JSON. </p>
  
  <h2>Here is my function:</h2>
  
  <pre><code>import json
      def lambda_handler(event, context):
          print("Received event: " + json.dumps(event, indent=2)) 
          message = event['Records'][0]['Sns']['Message']
          print(message)
  </code></pre>
  
  <h2>The event got from Cloudwatch:</h2>
  
  <pre><code>"Records": [
  {
  "EventVersion": "1.0", 
  "EventSubscriptionArn": "arn:aws:sns:us-east-1:xxxxxxxxxxxxx:bhuvi:XXXXXXXXXXXXXXXXXXXXXXXXXX", 
  "EventSource": "aws:sns", 
  "Sns": {
  "SignatureVersion": "1", 
  "Timestamp": "2018-01-13T19:18:44.369Z", 
  "Signature": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
  "SigningCertUrl": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem", 
  "MessageId": "4b76b0ea-5e0f-502f-81ec-e23e03dbaf01", 
  "Message": "{\"AlarmName\":\"test\",\"AlarmDescription\":\"test\",\"AWSAccountId\":\"xxxxxxxxxxxxx\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 out of the last 1 datapoints [2.6260535333900545 (13/01/18 19:13:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -&gt; ALARM transition).\",\"StateChangeTime\":\"2018-01-13T19:18:44.312+0000\",\"Region\":\"US East (N. Virginia)\",\"OldStateValue\":\"OK\",\"Trigger\":{\"MetricName\":\"CPUUtilization\",\"Namespace\":\"AWS/RDS\",\"StatisticType\":\"Statistic\",\"Statistic\":\"AVERAGE\",\"Unit\":null,\"Dimensions\":[{\"name\":\"DBInstanceIdentifier\",\"value\":\"myrds\"}],\"Period\":300,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"Threshold\":1.0,\"TreatMissingData\":\"\",\"EvaluateLowSampleCountPercentile\":\"\"}}", 
  "MessageAttributes":
  {}
  , 
  "Type": "Notification", 
  "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?xcsgagrgrwgwrg", 
  "TopicArn": "arn:aws:sns:us-east-1:xxxxxxxxxxxxx:bhuvi", 
  "Subject": "ALARM: \"test\" in US East (N. Virginia)"
  }
  }
  ]
  }
  </code></pre>
  
  <h2>My extract command(Upto message) and its result:</h2>
  
  <pre><code>message = event['Records'][0]['Sns']['Message']
          print(message)
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <pre><code>{
      "AlarmName": "test",
      "AlarmDescription": "test",
      "AWSAccountId": "xxxxxxxxxxxxx",
      "NewStateValue": "ALARM",
      "NewStateReason": "Threshold Crossed: 1 out of the last 1 datapoints [2.6260535333900545 (13/01/18 19:13:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -&gt; ALARM transition).",
      "StateChangeTime": "2018-01-13T19:18:44.312+0000",
      "Region": "US East (N. Virginia)",
      "OldStateValue": "OK",
      "Trigger": {
          "MetricName": "CPUUtilization",
          "Namespace": "AWS/RDS",
          "StatisticType": "Statistic",
          "Statistic": "AVERAGE",
          "Unit": null,
          "Dimensions": [
              {
                  "name": "DBInstanceIdentifier",
                  "value": "myrds"
              }
          ],
          "Period": 300,
          "EvaluationPeriods": 1,
          "ComparisonOperator": "GreaterThanOrEqualToThreshold",
          "Threshold": 1,
          "TreatMissingData": "",
          "EvaluateLowSampleCountPercentile": ""
      }
  </code></pre>
  
  <p>I want to extract some values from this message pane.</p>
  
  <p>For eg: I want to extract name. So I tried the below command, but unfortunately its not working. Can anyone help me on this? 
  <a href="https://i.stack.imgur.com/rCX0j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rCX0j.png" alt="enter image description here"></a></p>
  
  <h2>my code for this:</h2>
  
  <pre><code>message = event['Records'][0]['Sns']['Message']['Trigger']['Dimensions']['name']
      print(message)
  </code></pre>
  
  <h1>ERROR:</h1>
  
  <pre><code>{
    "stackTrace": [
      [
        "/var/task/lambda_function.py",
        14,
        "lambda_handler",
        "message = event['Records'][0]['Sns']['Message']['Trigger']['Dimensions']['name']"
      ]
    ],
    "errorType": "TypeError",
    "errorMessage": "string indices must be integers"
  }
  </code></pre>