1

I would like to filter a specific object from an array using a JSONPath filter expression within an AWS Step Function.

The JSONPath expression: $.IncidentRecordSummaries[?(@.Title==$.TitleFilter)].Arn

The data to filter on:

{
  "TitleFilter": "ALARM [hello]",
  "IncidentRecordSummaries": [
    {
      "Arn": "arn:aws:ssm-incidents::12345:incident-record/foo/abc",
      "CreationTime": "2023-02-23T16:54:34.241Z",
      "Impact": 3,
      "IncidentRecordSource": {
        "CreatedBy": "arn:aws:sts::12345:assumed-role/cs-mon-incidents-ms-ac-cloudwatch-sfn/step-functions-express-123",
        "InvokedBy": "states.amazonaws.com",
        "Source": "aws.cloudwatch"
      },
      "Status": "OPEN",
      "Title": "ALARM [hello]"
    },
    {
      "Arn": "arn:aws:ssm-incidents::12345:incident-record/bar/def",
      "CreationTime": "2023-02-23T16:25:51.772Z",
      "Impact": 3,
      "IncidentRecordSource": {
        "CreatedBy": "arn:aws:sts::12345:assumed-role/cs-mon-incidents-ms-pest-devops-guru-sfn/step-functions-express-456",
        "InvokedBy": "states.amazonaws.com",
        "Source": "aws.cloudwatch"
      },
      "Status": "OPEN",
      "Title": "ALARM [world]"
    }
  ]
}

Expected result:

["arn:aws:ssm-incidents::12345:incident-record/foo/abc"`]

Actual result: []

Any suggestion how to solve this issue?

The filter is working with a string literal being used in the expression: $.IncidentRecordSummaries[?(@.Title=='ALARM [hello]')].Arn returns the expected result.

0

1 Answer 1

1

This form of filter expression should work for Parameters, ResultSelector, InputPath, or OutputPath. See an example below using Pass states and Parameters.

{
  "StartAt": "Generate Input",
  "States": {
    "Generate Input": {
      "Type": "Pass",
      "Result": {
        "TitleFilter": "ALARM [hello]",
        "IncidentRecordSummaries": [
          {
            "Arn": "arn:aws:ssm-incidents::12345:incident-record/foo/abc",
            "CreationTime": "2023-02-23T16:54:34.241Z",
            "Impact": 3,
            "IncidentRecordSource": {
              "CreatedBy": "arn:aws:sts::12345:assumed-role/cs-mon-incidents-ms-ac-cloudwatch-sfn/step-functions-express-123",
              "InvokedBy": "states.amazonaws.com",
              "Source": "aws.cloudwatch"
            },
            "Status": "OPEN",
            "Title": "ALARM [hello]"
          },
          {
            "Arn": "arn:aws:ssm-incidents::12345:incident-record/bar/def",
            "CreationTime": "2023-02-23T16:25:51.772Z",
            "Impact": 3,
            "IncidentRecordSource": {
              "CreatedBy": "arn:aws:sts::12345:assumed-role/cs-mon-incidents-ms-pest-devops-guru-sfn/step-functions-express-456",
              "InvokedBy": "states.amazonaws.com",
              "Source": "aws.cloudwatch"
            },
            "Status": "OPEN",
            "Title": "ALARM [world]"
          }
        ]
      },
      "Next": "Extract Arns"
    },
    "Extract Arns": {
      "Type": "Pass",
      "End": true,
      "Parameters": {
        "key.$": "$.IncidentRecordSummaries[?(@.Title==$.TitleFilter)].Arn"
      }
    }
  }
}

When you run this, you will get the following output:

{
  "key": [
    "arn:aws:ssm-incidents::12345:incident-record/foo/abc"
  ]
}

I wonder if you might be using this in a Task state after calling an API using AWS SDK Service Integrations. In which case, the response you get back is the full API response where the data you want is under the Payload key. In which case you need you need to use $.Payload.IncidentRecordSummaries[?(@.Title==$.Payload.TitleFilter)].Arn instead of $.IncidentRecordSummaries[?(@.Title==$.TitleFilter)].Arn

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.