Spring Boot Apache Camel中JQ函数IN/1不存在问题求助
JQ表达式兼容问题解决方案
问题描述
尝试在Spring Boot Apache Camel 4.0.0(搭配同版本camel-jq)中使用以下JQ表达式筛选数据:
[.links[] | select(.from == "product1") | .to] as $colors | [.hits[] | select(.id | IN($colors[])) | .name]
但遇到两个报错:
- 使用大写
IN时,提示“Function IN/1 does not exist” - 改用小写
in时,提示“has() is not applicable to STRING”
等价替代方案
由于camel-jq依赖的JQ版本可能未支持IN/1函数,且小写in仅适用于判断对象键的存在,可采用以下两种替代写法:
方案1:使用index/1函数
利用index()判断元素是否存在于数组中(存在则返回索引值,不存在返回null):
[.links[] | select(.from == "product1") | .to] as $colors | [.hits[] | select(.id | index($colors) != null) | .name]
方案2:使用any/2函数遍历判断
通过any()遍历目标数组,匹配相等元素:
[.links[] | select(.from == "product1") | .to] as $colors | [.hits[] | select(any($colors[]; . == .id)) | .name]
内容的提问来源于stack exchange,提问作者Antônio
相关产品推荐
相关产品推荐

