Zeussi Zeussi - 5 days ago 5
JSON Question

how to split a json array in apache camel

I get a json array as input from a rest-interface, based ond apache-camel-spark. At the beginning, i want to split the json-array to process each element individual through apache camels routes. How i do this?

My test-input-json:

[
{
"document": {
"name": "blub1",
"type": "pdf"
}
},
{
"document": {
"name": "blub2",
"type": "pdf"
}
}
];


For this problem, i found some questions on stackoverflow, which describe this indirectly:

link 1, link 2, link 3.

Based on the examples, i tried the following camel-routes:

restConfiguration().component("spark-rest").port(8080)
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true");

rest("/v1/users").consumes("application/json").produces("application/json")
.post("/insert")
.to("direct:split");

from("direct:split")
.marshal().json(JsonLibrary.Jackson)
.split(new JsonPathExpression("$..document.")).process(new Processor() {
public void process(Exchange exchange) throws Exception {
String s = exchange.getIn().getBody(String.class);
System.out.println(s);
}
}).to("file:outbox");


When i do this, i get always the following exception:


ERROR DefaultErrorHandler - Failed delivery for (MessageId:
[...] on ExchangeId: [...]). Exhausted after delivery attempt: 1
caught: org.apache.camel.ExpressionEvaluationException:
java.lang.NullPointerException


I didn't found the reason, whats wong with my expression? I changed it, but i always get the NullPointerException.

I downloaded the JavaScript jsonPath library and tested the
$..document.
expression, which serves me an array with the two objects. I also tried it in camel, which says, that the expression was found (returns true) in the test-input-json:

from("direct:testExpression")
.marshal().json(JsonLibrary.Jackson)
.choice()
.when().jsonpath("$..document.", true)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
String payload = exchange.getIn().getBody(String.class);
exchange.getIn().setBody("true");
System.out.println(payload);
}
})
.to("file:outbox")
.otherwise()
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
String payload = exchange.getIn().getBody(String.class);
exchange.getIn().setBody("false");
System.out.println(payload);
}
})
.to("file:outbox");


What do i wrong, why i get the NullPointerException and how do i split the json?

Here the full Stacktrace:

org.apache.camel.ExpressionEvaluationException: java.lang.NullPointerException
at org.apache.camel.jsonpath.JsonPathExpression.evaluate(JsonPathExpression.java:95) ~[camel-jsonpath-2.18.0.jar:2.18.0]
at org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:36) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.Splitter.createProcessorExchangePairs(Splitter.java:113) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.MulticastProcessor.process(MulticastProcessor.java:231) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.Splitter.process(Splitter.java:108) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) [camel-core-2.18.0.jar:2.18.0]
at org.apache.camel.component.sparkrest.CamelSparkRoute.handle(CamelSparkRoute.java:46) [camel-spark-rest-2.18.0.jar:2.18.0]
at spark.RouteImpl$1.handle(RouteImpl.java:58) [spark-core-2.3.jar:?]
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:162) [spark-core-2.3.jar:?]
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61) [spark-core-2.3.jar:?]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.Server.handle(Server.java:499) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544) [jetty-io-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.19.v20160908.jar:9.2.19.v20160908]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.19.v20160908.jar:9.2.19.v20160908]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
Caused by: java.lang.NullPointerException
at org.apache.camel.jsonpath.JsonPathExpression.evaluateJsonPath(JsonPathExpression.java:118) ~[camel-jsonpath-2.18.0.jar:2.18.0]
at org.apache.camel.jsonpath.JsonPathExpression.evaluate(JsonPathExpression.java:88) ~[camel-jsonpath-2.18.0.jar:2.18.0]
... 31 more

Answer

Try creating your jsonpath expressing similar to:

ExpressionBuilder.languageExpression("jsonpath","$..document.") 

and put that in your split and try. I have had more success with jsonpath this way when doing various conditional evaluations.

Comments