Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.

JSonPath

Available as of Camel 2.13

Camel supports JSonPath to allow using Expression or Predicate on JSON messages.

from("queue:books.new")
  .choice()
    .when().jsonpath("$.store.book[?(@.price < 10)]")
      .to("jms:queue:book.cheap")
    .when().jsonpath("$.store.book[?(@.price < 30)]")
      .to("jms:queue:book.average")
    .otherwise()
      .to("jms:queue:book.expensive")

Using XML configuration

If you prefer to configure your routes in your Spring XML file then you can use JSonPath expressions as follows:

  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
      <from uri="direct:start"/>
      <choice>
        <when>
          <jsonpath>$.store.book[?(@.price &lt; 10)]</jsonpath>
          <to uri="mock:cheap"/>
        </when>
        <when>
          <jsonpath>$.store.book[?(@.price &lt; 30)]</jsonpath>
          <to uri="mock:average"/>
        </when>
        <otherwise>
          <to uri="mock:expensive"/>
        </otherwise>
      </choice>
    </route>
  </camelContext>

Syntax

See the JSonPath project page for further examples.

Suppress exceptions

Available as of Camel 2.16

By default jsonpath will throw an exception if the JSON payload does not have a valid path accordingly to the configured jsonpath expression. In some use-cases you may want to ignore this in case the JSON payload contains optional data. To ignore exceptions set the option suppressExceptions=true as shown below:

from("direct:start")
    .choice()
        // use true to suppress exceptions
        .when().jsonpath("person.middlename", true)
            .to("mock:middle")
        .otherwise()
            .to("mock:other");

And in XML DSL:

    <route>
      <from uri="direct:start"/>
      <choice>
        <when>
          <jsonpath suppressExceptions="true">person.middlename</jsonpath>
          <to uri="mock:middle"/>
        </when>
        <otherwise>
          <to uri="mock:other"/>
        </otherwise>
      </choice>
    </route>

 

This option is also available on the @JsonPath annotation.

Inline Simple expressions

Available as of Camel 2.18

It's now possible to in-lined Simple language expressions in the JSonPath expression using the simple syntax ${xxx}.

Example:

from("direct:start")
  .choice()
    .when().jsonpath("$.store.book[?(@.price < ${header.cheap})]")
      .to("mock:cheap")
    .when().jsonpath("$.store.book[?(@.price < ${header.average})]")
      .to("mock:average")
    .otherwise()
      .to("mock:expensive");

In this example the Simple expression in-lined is the headers with the cheap and average values to be used. 


    <route>
      <from uri="direct:start"/>
      <choice>
        <when>
          <jsonpath>$.store.book[?(@.price < ${header.cheap})]</jsonpath>
          <to uri="mock:cheap"/>
        </when>
        <when>
          <jsonpath>$.store.book[?(@.price < ${header.average})]</jsonpath>
          <to uri="mock:average"/>
        </when>
        <otherwise>
          <to uri="mock:expensive"/>
        </otherwise>
      </choice>
    </route>

You can turn off support for in-lined simple expression by setting the option allowSimple=false as shown:

 

 

 

// java dsl
.when().jsonpath("$.store.book[?(@.price < 10)]", false, false)
 
// xml dsl
<jsonpath allowSimple="false">$.store.book[?(@.price < 10)]</jsonpath>

 

 

 

JSonPath injection

You can use Bean Integration to invoke a method on a bean and use various languages such as JSonPath to extract a value from the message and bind it to a method parameter.

Example:

public class Foo {
	
    @Consume(uri = "activemq:queue:books.new")
    public void doSomething(@JsonPath("$.store.book[*].author") String author, @Body String json) {
      // process the inbound message here
    }
}

Encoding Detection

Since Camel version 2.16, the encoding of the JSON document is detected automatically, if the document is encoded in Unicode  (UTF-8, UTF-16LE, UTF-16BE, UTF-32LEUTF-32BE) as specified in RFC-4627. If the encoding is a non-Unicode encoding, you can either make sure that you enter the document in String format to the JSonPath component or you can specify the encoding in the header CamelJsonPathJsonEncoding (JsonpathConstants.HEADER_JSON_ENCODING).

Dependencies

To use JSonPath in your camel routes you need to add the a dependency on camel-jsonpath which implements the JSonPath language.

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jsonpath</artifactId>
  <version>x.x.x</version>
</dependency>
© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram