This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.10.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 8e27630db1fd132da8d321cec3bbbf4d61c10148 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jun 18 22:29:34 2025 +0200 CAMEL-22180: camel-simple - Using inlined jsonpath with 2-arg exp should trim so you can use space around comma --- .../src/main/docs/jsonpath-language.adoc | 18 ++++++++++++++++++ .../apache/camel/jsonpath/JsonPathVariableTest.java | 2 +- .../language/simple/ast/SimpleFunctionExpression.java | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc b/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc index 34cdcdb849f..08f9d30a4cc 100644 --- a/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc +++ b/components/camel-jsonpath/src/main/docs/jsonpath-language.adoc @@ -252,6 +252,24 @@ XML DSL:: ==== +=== Using variables as source + +By default, the message body is the source for the jsonpath evaluation. +However, if you need to refer to a variable or message header instead as the body, +then this is easy as shown below: + +[source,java] +---- +from("direct:start") + .setVariable("cars", constant("[\"Ford\", \"BMW\", \"Fiat\"]")) + .setBody(simple("${jsonpath(variable:cars , $.length())}")) + .to("mock:cars"); +---- + +Here we set a variable named _cars_ with a json array of 3 cars. Then we want to count the length +of the array using jsonpath length function. Notice how we use the inlined simple language, and can +easily refer to the variable as the source using `variable:cars`. + === JSONPath injection You can use xref:manual::bean-integration.adoc[Bean Integration] to invoke a diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathVariableTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathVariableTest.java index aaa1fcf51d8..80dca8f0c11 100644 --- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathVariableTest.java +++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathVariableTest.java @@ -30,7 +30,7 @@ public class JsonPathVariableTest extends CamelTestSupport { public void configure() { from("direct:start") .setVariable("cars", constant("[\"Ford\", \"BMW\", \"Fiat\"]")) - .setBody(simple("${jsonpath(variable:cars,$.length())}")) + .setBody(simple("${jsonpath(variable:cars , $.length())}")) .to("mock:result"); } }; diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java index e56ce2b9218..ab3c7a08810 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java @@ -570,6 +570,12 @@ public class SimpleFunctionExpression extends LiteralExpression { || exp.startsWith("variable:")) { String input = StringHelper.before(exp, ","); exp = StringHelper.after(exp, ","); + if (input != null) { + input = input.trim(); + } + if (exp != null) { + exp = exp.trim(); + } return ExpressionBuilder.singleInputLanguageExpression("jq", exp, input); } return ExpressionBuilder.languageExpression("jq", exp); @@ -586,6 +592,12 @@ public class SimpleFunctionExpression extends LiteralExpression { || exp.startsWith("variable:")) { String input = StringHelper.before(exp, ","); exp = StringHelper.after(exp, ","); + if (input != null) { + input = input.trim(); + } + if (exp != null) { + exp = exp.trim(); + } return ExpressionBuilder.singleInputLanguageExpression("jsonpath", exp, input); } return ExpressionBuilder.languageExpression("jsonpath", exp); @@ -601,6 +613,12 @@ public class SimpleFunctionExpression extends LiteralExpression { || exp.startsWith("variable:")) { String input = StringHelper.before(exp, ","); exp = StringHelper.after(exp, ","); + if (input != null) { + input = input.trim(); + } + if (exp != null) { + exp = exp.trim(); + } return ExpressionBuilder.singleInputLanguageExpression("xpath", exp, input); } return ExpressionBuilder.languageExpression("xpath", exp);