jsonpathaws-step-functionsjayway

AWS Step Functions: Filter an array using JsonPath


I need to filter an array in my AWS Step Functions state. This seems like something I should easily be able to achieve with JsonPath but I am struggling for some reason.

The state I want to process looks like this:

{
  "items": [
    {
      "id": "A"
    },
    {
      "id": "B"
    },
    {
      "id": "C"
    }
  ]
}

I want to filter this array by removing entries for which id is not in a specified whitelist.

To do this, I define a Pass state in the following way:

"ApplyFilter": {
  "Type": "Pass",
  "ResultPath": "$.items",
  "InputPath": "$.items.[?(@.id in ['A'])]",
  "Next": "MapDeployments"
}

This makes use of the JsonPath in operator.

Unfortunately when I execute the state machine I receive an error:

{
  "error": "States.Runtime",
  "cause": "An error occurred while executing the state 'ApplyFilter' (entered at the event id #8). Invalid path '$.items.[?(@.id in ['A'])]' : com.jayway.jsonpath.InvalidPathException: com.jayway.jsonpath.InvalidPathException: Space not allowed in path"
}

However, I don't understand what is incorrect with the syntax. When I test here everything works correctly.

What is wrong with what I have done? Is there another way of achieving this sort of filter using JsonPath?


Solution

  • You can use regular expression evaluation as alternatives. Note that you don't need to put a dot between "items" and the following square brackets.

    "ApplyFilter": {
      "Type": "Pass",
      "ResultPath": "$.items",
      "InputPath": "$.items[?(@.id =~ /A|C/)]",
      "Next": "MapDeployments"
    }
    

    You can also use expressions like /.*A.*|.*C.*/, if you want partial match. Explore this link for other useful expressions.

    On the other hand, based on the testing I did today, in expressions seem to be unsupported as of now.