This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new cff04dd73f93 chore: docs - jsonpath filter returning the single match 
or null (#27297)
cff04dd73f93 is described below

commit cff04dd73f938f29b8ad2e94955682040920c90f
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Oct 2 21:13:42 2026 +0200

    chore: docs - jsonpath filter returning the single match or null (#27297)
    
    Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
---
 .../camel/catalog/docs/jsonpath-language.adoc      | 96 ++++++++++++++++++++++
 .../src/main/docs/jsonpath-language.adoc           | 96 ++++++++++++++++++++++
 2 files changed, 192 insertions(+)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jsonpath-language.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jsonpath-language.adoc
index 835d7515d479..500876ac409b 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jsonpath-language.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jsonpath-language.adoc
@@ -406,6 +406,102 @@ YAML::
 ----
 ====
 
+When the filter matches on a unique key, such as the SKU, you can let JsonPath 
return the element itself instead of a
+list: set `resultType` to `java.util.Map` (or another non-collection type). A 
single match is then returned as the
+element, and no match returns `null`, so a `null` check replaces the size 
check and the `${body[0]}`:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:one-sku")
+    .setBody(constant("resource:file:stock.json"))
+    .setBody().jsonpath("$[?(@.sku == '${header.sku}')]", Map.class)
+    .choice()
+        .when(body().isNull())
+            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(404))
+            .setBody(simple("unknown SKU ${header.sku}"))
+        .otherwise()
+            .marshal().json()
+    .end();
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+  <from uri="direct:one-sku"/>
+  <setBody>
+    <constant>resource:file:stock.json</constant>
+  </setBody>
+  <setBody>
+    <jsonpath resultType="java.util.Map">$[?(@.sku == 
'${header.sku}')]</jsonpath>
+  </setBody>
+  <choice>
+    <when>
+      <simple>${body} == null</simple>
+      <setHeader name="CamelHttpResponseCode">
+        <constant>404</constant>
+      </setHeader>
+      <setBody>
+        <simple>unknown SKU ${header.sku}</simple>
+      </setBody>
+    </when>
+    <otherwise>
+      <marshal>
+        <json/>
+      </marshal>
+    </otherwise>
+  </choice>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+    from:
+      uri: direct:one-sku
+      steps:
+        - setBody:
+            expression:
+              constant:
+                expression: resource:file:stock.json
+        - setBody:
+            expression:
+              jsonpath:
+                expression: "$[?(@.sku == '${header.sku}')]"
+                resultType: java.util.Map
+        - choice:
+            when:
+              - expression:
+                  simple:
+                    expression: "${body} == null"
+                steps:
+                  - setHeader:
+                      name: CamelHttpResponseCode
+                      expression:
+                        constant:
+                          expression: "404"
+                  - setBody:
+                      expression:
+                        simple:
+                          expression: "unknown SKU ${header.sku}"
+            otherwise:
+              steps:
+                - marshal:
+                    json: {}
+----
+====
+
+NOTE: When the filter matches more than one element, a non-collection 
`resultType` also returns `null`, so use
+this form only when at most one element can match. The `unpackArray` option 
does not help here: it unpacks a
+single-element list, but leaves an empty list as an empty list.
+
 You can turn off support for inlined Simple expression by setting the option 
`allowSimple` to `false` as shown:
 
 [tabs]
diff --git a/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc 
b/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc
index 835d7515d479..500876ac409b 100644
--- a/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc
+++ b/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc
@@ -406,6 +406,102 @@ YAML::
 ----
 ====
 
+When the filter matches on a unique key, such as the SKU, you can let JsonPath 
return the element itself instead of a
+list: set `resultType` to `java.util.Map` (or another non-collection type). A 
single match is then returned as the
+element, and no match returns `null`, so a `null` check replaces the size 
check and the `${body[0]}`:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:one-sku")
+    .setBody(constant("resource:file:stock.json"))
+    .setBody().jsonpath("$[?(@.sku == '${header.sku}')]", Map.class)
+    .choice()
+        .when(body().isNull())
+            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(404))
+            .setBody(simple("unknown SKU ${header.sku}"))
+        .otherwise()
+            .marshal().json()
+    .end();
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+  <from uri="direct:one-sku"/>
+  <setBody>
+    <constant>resource:file:stock.json</constant>
+  </setBody>
+  <setBody>
+    <jsonpath resultType="java.util.Map">$[?(@.sku == 
'${header.sku}')]</jsonpath>
+  </setBody>
+  <choice>
+    <when>
+      <simple>${body} == null</simple>
+      <setHeader name="CamelHttpResponseCode">
+        <constant>404</constant>
+      </setHeader>
+      <setBody>
+        <simple>unknown SKU ${header.sku}</simple>
+      </setBody>
+    </when>
+    <otherwise>
+      <marshal>
+        <json/>
+      </marshal>
+    </otherwise>
+  </choice>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+    from:
+      uri: direct:one-sku
+      steps:
+        - setBody:
+            expression:
+              constant:
+                expression: resource:file:stock.json
+        - setBody:
+            expression:
+              jsonpath:
+                expression: "$[?(@.sku == '${header.sku}')]"
+                resultType: java.util.Map
+        - choice:
+            when:
+              - expression:
+                  simple:
+                    expression: "${body} == null"
+                steps:
+                  - setHeader:
+                      name: CamelHttpResponseCode
+                      expression:
+                        constant:
+                          expression: "404"
+                  - setBody:
+                      expression:
+                        simple:
+                          expression: "unknown SKU ${header.sku}"
+            otherwise:
+              steps:
+                - marshal:
+                    json: {}
+----
+====
+
+NOTE: When the filter matches more than one element, a non-collection 
`resultType` also returns `null`, so use
+this form only when at most one element can match. The `unpackArray` option 
does not help here: it unpacks a
+single-element list, but leaves an empty list as an empty list.
+
 You can turn off support for inlined Simple expression by setting the option 
`allowSimple` to `false` as shown:
 
 [tabs]

Reply via email to