이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 188. JsonPath Language


Available as of Camel version 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")

188.1. JSonPath Options

The JsonPath language supports 7 options which are listed below.

NameDefaultJava TypeDescription

resultType

 

String

Sets the class name of the result type (type from output)

suppressExceptions

false

Boolean

Whether to suppress exceptions such as PathNotFoundException.

allowSimple

true

Boolean

Whether to allow in inlined simple exceptions in the JsonPath expression

allowEasyPredicate

true

Boolean

Whether to allow using the easy predicate parser to pre-parse predicates.

writeAsString

false

Boolean

Whether to write the output of each row/element as a JSON String value instead of a Map/POJO value.

headerName

 

String

Name of header to use as input, instead of the message body

trim

true

Boolean

Whether to trim the value to remove leading and trailing whitespaces and line breaks

188.2. 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>

188.3. Syntax

See the JSonPath project page for further examples.

188.4. Easy Syntax

Available as of Camel 2.19

When you just want to define a basic predicate using jsonpath syntax it can be a bit hard to remember the syntax. So for example to find out all the cheap books you have to do

$.store.book[?(@.price < 20)]

However what if you could just write it as

store.book.price < 20

And you can omit the path if you just want to look at nodes with a price key

price < 20

To support this there is a EasyPredicateParser which kicks-in if you have define the predicate using a basic style. That means the predicate must not start with the $ sign, and only include one operator.

The easy syntax is:

left OP right

You can use Camel simple language in the right operator, eg

store.book.price < ${header.limit}

188.5. Supported message body types

Camel JSonPath supports message body using the following types:

TypeComment

File

Reading from files

String

Plain strings

Map

Message bodies as java.util.Map types

List

Message bodies as java.util.List types

POJO

Optional If Jackson is on the classpath, then camel-jsonpath is able to use Jackson to read the message body as POJO and convert to java.util.Map which is supported by JSonPath. For example you can add camel-jackson as dependency to include Jackson.

InputStream

If none of the above types matches, then Camel will attempt to read the message body as an java.io.InputStream.

If a message body is of unsupported type then an exception is thrown by default, however you can configure JSonPath to suppress exceptions (see below)

188.6. 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. Therefore you can set the option suppressExceptions to true to ignore this as shown:

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.

188.7. Inline Simple exceptions

Available as of Camel 2.18

Its now possible to inlined Simple language expressions in the JSonPath expression using the simple syntax ${xxx}. An example is shown below:

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");

And in XML DSL:

<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 inlined simple expression by setting the option allowSimple to false as shown:

.when().jsonpath("$.store.book[?(@.price < 10)]", false, false)

And in XML DSL:

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

188.8. 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.

For 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
    }
}

188.9. 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-32LE, UTF-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).

188.10. Split JSon data into sub rows as JSon

You can use jsonpath to split a JSon document, such as:

from("direct:start")
    .split().jsonpath("$.store.book[*]")
    .to("log:book");

Then each book is logged, however the message body is a Map instance. Sometimes you may want to output this as plain String JSon value instead, which can be done from Camel 2.20 onwards with the writeAsString option as shown:

from("direct:start")
    .split().jsonpathWriteAsString("$.store.book[*]")
    .to("log:book");

Then each book is logged as a String JSon value. For earlier versions of Camel you would need to use camel-jackson dataformat and marshal the message body to make it convert the message body from Map to a String type.

188.11. Using header as input

Available as of Camel 2.20

By default jsonpath uses the message body as the input source. However you can also use a header as input by specifying the headerName option.

For example to count the number of books from a json document that was stored in a header named books you can do:

from("direct:start")
    .setHeader("numberOfBooks")
        .jsonpath("$..store.book.length()", false, int.class, "books")
    .to("mock:result");

In the jsonpath expression above we specify the name of the header as books and we also told that we wanted the result to be converted as an integer by int.class.

The same example in XML DSL would be:

<route>
  <from uri="direct:start"/>
  <setHeader headerName="numberOfBooks">
    <jsonpath headerName="books" resultType="int">$..store.book.length()</jsonpath>
  </transform>
  <to uri="mock:result"/>
</route>

188.12. 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>
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.