Apache Camel中JSONPath筛选new.status=Start的delta节点及表达式排障
Let's break down why your existing expressions aren't working and how to fix them, using your sample JSON input:
[{ "delta": { "status": { "old": { "status": "Stop" }, "new": { "status": "Start" } } } }]
Why Your Current Expressions Fail
1. $..[?(@.delta.status.new.status=='Start')]
This expression targets the array element (the parent object containing the delta node), not the delta node itself. While it might return a non-empty result (which should trigger the when condition), it doesn't give you the specific delta node you need for further processing.
2. $..delta.status.new[?(@.status=='Start')]
This expression selects the new sub-node (where status is Start), not the delta node. Again, this doesn't align with your goal of retrieving the full delta object.
Additionally, a common pitfall in Java code is using unescaped double quotes around string literals (like "Start"), which breaks the Java string syntax. Always use single quotes for JSONPath string values to avoid this issue.
Correct JSONPath Expressions to Get the Delta Node
Choose one of these expressions based on your JSON structure:
Option 1: Precise Root-to-Delta Path (Recommended for Fixed Structures)
This filters the root array for elements where delta.status.new.status equals Start, then extracts the delta node directly:
$[?(@.delta.status.new.status=='Start')].delta
Option 2: Recursive Delta Search (Flexible for Variable Structures)
This recursively finds all delta nodes in the JSON, then filters those where their nested status.new.status is Start:
$..delta[?(@.status.new.status=='Start')]
Implementation in Apache Camel
Here's how to use the recursive expression in your route (note the single quotes around Start to avoid Java syntax errors):
from("your-input-source") .choice() .when().jsonpath("$..delta[?(@.status.new.status=='Start')]") .log("Processing delta node with new status Start: ${body}") // Add your custom processing logic here .otherwise() .log("No matching delta node found") .end();
Additional Checks
- Ensure your JSON input is properly parsed: If your input is a raw string, Camel should auto-convert it for JSONPath evaluation, but you can explicitly unmarshal it using Jackson if needed.
- Confirm compatibility: Apache Camel uses the Jayway JSONPath library under the hood, so expressions that work on jsonpath.com should work here (just adjust quote syntax for Java).
内容的提问来源于stack exchange,提问作者millka_15

