AWS Step Functions:使用 JsonPath 过滤数组

问题描述

我需要在我的 AWS Step Functions 状态中过滤一个数组。这似乎是我应该可以通过 JsonPath 轻松实现的事情,但由于某种原因我正在努力。

我要处理的状态如下所示:

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

我想通过删除 id 不在指定白名单中的条目来过滤此数组。

为此,我通过以下方式定义了 Pass 状态:

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

这使用了 JsonPath in operator

不幸的是,当我执行状态机时,我收到一个错误

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

但是,我不明白语法有什么不正确。当我测试 here 时,一切正常。

我所做的有什么问题?是否有另一种使用 JsonPath 实现这种过滤器的方法

解决方法

根据 Step Functions 的官方 AWS 文档,

不支持路径中的以下@ ..,: ? *

https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html