我需要在AWS Step Functions状态中过滤一个数组。这似乎是我应该很容易用JsonPath实现的事情,但出于某种原因我感到困难。
我想要处理的状态看起来像这样:
{
"items": [
{
"id": "A"
},
{
"id": "B"
},
{
"id": "C"
}
]
}
我希望能够通过白名单过滤掉数组中不在指定白名单中的条目。
为了实现这一目的,我定义了一个“Pass”状态,具体定义如下:
"ApplyFilter": {
"Type": "Pass",
"ResultPath": "$.items",
"InputPath": "$.items.[?(@.id in ['A'])]",
"Next": "MapDeployments"
}
这利用了JsonPath
in
运算符。
不幸的是,当我执行状态机时收到一个错误:
{
"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"
}
然而,我不明白语法有什么问题。当我在这里测试时,一切都能正常运行。
我做错了什么?是否有其他方法可以使用JsonPath来实现这种类型的筛选?
in
运算符,但实际上这种情况很少见,因为这个功能已经存在了相当长的时间。也许是Pass过滤器的问题。你可以尝试反转逻辑并使用nin
运算符吗? - wp78denin
也不起作用。但是==
起作用。我认为你是对的,AWS 出于某种原因在使用一个非常旧的 JsonPath 版本。从我所看到的来看,in
和nin
自 2015 年发布的 2.1.0 版本以来就可用。 - timxyz