"list indices must be integers or slices, not str" ERROR

Question:

For some reason, I keep getting this error from AWS Lambda:

{
  "errorMessage": "list indices must be integers or slices, not str",
  "errorType": "TypeError",
  "stackTrace": [
    "  File "/var/task/python.py", line 25, in lambda_handlern    metricName = event['detail']['configuration']['metrics']['metricStat']['metric']['name']n"
  ]
}

The following part is the JSON part for what I am trying to reference to:

"configuration": {
      "metrics": [
        {
          "id": "ajdhflksdf4",
          "metricStat": {
            "metric": {
              "namespace": "AWS/RDS",
              "name": "CPUUtilization",
              "dimensions": {
                "DBInstanceIdentifier": "123456-rds-tester"
              }
            },
            "period": 60,
            "stat": "Average"
          },`
          "returnData": "True"
        }
      ],
      "description": "This metric monitors rds cpu utilization"
    }

I want to reference the name in the metric part and all of that is under the key ‘detail’. I was expecting to receive "CPUUtilization" for metricName

Asked By: John Kennedy

||

Answers:

The value associated with the key metrics is a list. You should access ...['metrics'][0]['metricStat']... instead of ...['metrics']['metricStat']..., as the sub-dictionary you wish to access is stored in the 0th index of the list.

Answered By: Ryan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.