このコンテンツは選択した言語では利用できません。

Chapter 20. JSonPath


Overview

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:
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jsonpath</artifactId>
  <version>${camel-version}</version>
</dependency>
Copy to Clipboard Toggle word wrap

Java example

The following Java example shows how to use the jsonpath() DSL command to select items in a certain price range:
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")
Copy to Clipboard Toggle word wrap
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="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>
Copy to Clipboard Toggle word wrap

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 = "activemq:queue:books.new")
    public void doSomething(@JsonPath("$.store.book[*].author") String author, @Body String json) {
      // process the inbound message here
    }
}
Copy to Clipboard Toggle word wrap

Reference

For more details about JSonPath, see the JSonPath project page.
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat