用于检查 Apache Camel XML 中字符串的 JSONPath 表达式 [英] JSONPath expression for checking string in Apache Camel XML
问题描述
假设我有一个简单的 json 文件,如下所示
Let's say I have a simple json file such as the following
{
"log": {
"host": "blah",
"severity": "INFO",
"system": "1"
}
}
我使用的是 Apache Camel,它是 Spring XML 来处理和路由 json 文件.我的路由代码如下所示:
I'm using Apache Camel, and it's Spring XML to process and route the json file. My routing code looks something like this:
<route>
<from uri="file:/TESTFOLDER/input"/>
<choice>
<when>
<jsonpath>$.log?(@.severity == 'WARNING')</jsonpath>
<to uri="smtp://(smtpinfo...not important)"/>
</when>
<otherwise>
<to uri="file:/TESTFOLDER/output"/>
</otherwise>
</choice>
</route>
我真正感到困惑的部分是 JSONPath 表达式.我上面的表达式甚至在语法上都不正确,因为在您不尝试对元素列表进行排序的情况下,很难找到示例.我的目标是仅在日志的严重性为警告"时发送电子邮件,但我无法想出表达式.
The part that I'm really confused about is the JSONPath expression. The expression I have above isn't even syntactically correct, because its hard to find examples for the case where you aren't trying to sort through a list of elements. My goal is to only send an email if the severity of the log is 'WARNING' but I can't come up with the expression.
推荐答案
这对我使用 Camel 2.13.1 有用(我检查了 INFO
因为您的 JSON 示例具有此 severity代码>;您可以根据需要进行更改):
This worked for me using Camel 2.13.1 (I checked for INFO
as your JSON example has this severity
; you may change this according to your needs):
<jsonpath>$..log[?(@.severity == 'INFO')]</jsonpath>
注意 ..
和 []
.但是,在搜索路径的开头使用单点 .
失败:
Note the ..
and the []
. However, using a single dot .
at the beginning of the search path failed:
<jsonpath>$.log[?(@.severity == 'INFO')]</jsonpath>
错误信息说:
java.lang.IllegalArgumentException: Invalid container object
这可能是一个错误.
根据 JSON 路径 doc,..
代表递归下降".这可能不符合您的要求.但是,由于单点 .
不起作用,这是我想到的唯一可能的解决方法.否则,您可能会收到错误通知单.
According to the JSON Path doc, ..
stands for "recursive descent". This may not meet your requirements. However, as a single dot .
didn't work, this was the only possible work around I figured out. Otherwise, you may rise a bug ticket.
这篇关于用于检查 Apache Camel XML 中字符串的 JSONPath 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!