使用“jsonPath”过滤掉包含空数组的元素的表达式

blu*_*fus 2 java spring spring-integration jsonpath

我有一个以下形式的 JSON 有效负载:

[
  {"id": 1, "list": [1], "name":"one"}, 
  {"id": 2, "list": [1,2], "name":"two"},
  {"id": 3, "list": [], "name":"three"}
]
Run Code Online (Sandbox Code Playgroud)

我想从包含空属性的数组中过滤掉元素"list"。换句话说,我想丢弃该元素并id=3仅处理上面示例中的第一个和第二个元素。

目前,我的过滤器如下所示:

<!-- ne == not equals -->
<int:filter id="filter" 
            input-channel="in" 
            output-channel="out" 
            expression="#jsonPath(payload, '$[*].list') ne '[]'"
            discard-channel="consoleOutputChannel" />
Run Code Online (Sandbox Code Playgroud)

但这不起作用,我应该如何向 my 表明expression我想要排除具有空list属性的元素?

Vie*_*iet 5

将表达式更改为:

$.[?(@.list.length()> 0)]
Run Code Online (Sandbox Code Playgroud)
  • [?(<expression>)]: 过滤表达式
  • @ : 过滤谓词正在处理的当前节点
  • list.length()list:数组 的长度

更多细节请参见JsonPath