Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 20. JSonPath


The JSonPath language provides a convenient syntax for extracting portions of a JSon message. The syntax of JSon is similar to XPath, but it is used to extract JSon objects from a JSon message, instead of acting on XML. The jsonpath DSL command can be used either as an expression or as a predicate (where an empty result gets interpreted as boolean false).

Adding the JSonPath package

To use JSonPath in your Camel routes, you need to add a dependency on camel-jsonpath to your project, as follows:

Java example

The following Java example shows how to use the jsonpath() DSL command to select items in a certain price range:
    .when().jsonpath("$[?(@.price < 10)]")
    .when().jsonpath("$[?(@.price < 30)]")
If the JSonPath query returns an empty set, the result is interpreted as false. In this way, you can use a JSonPath query as a predicate.

XML example

The following XML example shows how to use the jsonpath DSL element to define predicates in a route:
<camelContext id="camel" xmlns="">
    <from uri="direct:start"/>
        <jsonpath>$[?(@.price &lt; 10)]</jsonpath>
        <to uri="mock:cheap"/>
        <jsonpath>$[?(@.price &lt; 30)]</jsonpath>
        <to uri="mock:average"/>
        <to uri="mock:expensive"/>

JSonPath injection

When using bean integration to invoke a bean method, you can use JSonPath to extract a value from the message and bind it to a method parameter. For example:
// Java
public class Foo {
    @Consume(uri = "")
    public void doSomething(@JsonPath("$[*].author") String author, @Body String json) {
      // process the inbound message here


For more details about JSonPath, see the JSonPath project page.