Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONPath expression for checking string in Apache Camel XML

Let's say I have a simple json file such as the following

{
    "log": {
        "host": "blah",
        "severity": "INFO",
        "system": "1"
    }
}

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>

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.

like image 563
Andrew G Avatar asked May 22 '14 20:05

Andrew G


1 Answers

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>

The error messages said:

java.lang.IllegalArgumentException: Invalid container object

This may be a bug.

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.

like image 63
Peter Keller Avatar answered Oct 19 '22 14:10

Peter Keller