You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Shabin5785 <sh...@outlook.com> on 2016/09/28 14:32:02 UTC

Parse Json inside RecipientList

Hi,

I have a message received as Json. Logging the message body gives me below
value (example)
{ "id":22, "type":"edit"}

I need to invoke a rest api which needs the id value from the above json to
be included. Api end point is like http://xxxxx/api/customer/<id>, where id
is the value above.

I configured the route like:

from("jms:queue") 
.marshal() 
.json(JsonLibrary.GSON) 
.setHeader(Exchange.CONTENT_TYPE,constant("application/json")) 
.setHeader(Exchange.HTTP_METHOD, constant("POST")) 
.recipientList( simple("https4://xxxxx/api/customer/<id>"))

How can i inject the id from the body of my message? There is an xpath
option which i can embed, but there seems to be no jsonpath option. I can
only link a jsonpath expression , but cannot put it inside recipientList.





--
View this message in context: http://camel.465427.n5.nabble.com/Parse-Json-inside-RecipientList-tp5788162.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Parse Json inside RecipientList

Posted by Brad Johnson <br...@mediadriver.com>.
To amplify on sparekh's answer I think with a POJO you could simply use:

"https4://xxxxx/api/customer/${body.id}"

On Wed, Sep 28, 2016 at 11:49 AM, sparekh <sp...@oneweb.net> wrote:

> How about a custom processor that parses the JSON string (via GSON) into a
> JSON object and sets up the HTTP request and headers. I don't believe you
> can use JsonPath to extract values, just choice or filter them.
>
> The custom processor would translate String -> JSON Object, extract the id
> from JSON object, set up CONTENT_TYPE, METHOD AND URI Headers, remove non
> HTTP headers. If you set the Exchange.HTTP_URI header you can remove the
> recipientList and have a to endpoint with a dummy value
> (.to("https4://dummy"))
>
> If the jms message is a serialized POJO then you can use the Camel OGNL
> expression to grab the idea via body.id
>
> Thanks,
> sparekh
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.
> com/Parse-Json-inside-RecipientList-tp5788162p5788164.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Parse Json inside RecipientList

Posted by Brad Johnson <br...@mediadriver.com>.
OK, cool. Glad that worked for you. I usually use a header simply because
it passes from route to route while the properties do not.

But in this case, the use of the properties is probably better since you
don't need the property to go anywhere else. But glad you got it working.

On Thu, Sep 29, 2016 at 11:00 PM, Shabin5785 <sh...@outlook.com> wrote:

> OK got it.. I added it as a property on exchange.
>
>
>
> Ranx wrote
> > There are a number of ways but perhaps the simplest would be something
> > like:
> >
> > .setHeader("customerId","${body.id}")
> >
> > right before the marshaling.  Then in the http statement where the ID
> goes
> > you can fish it out of the header with something like
> > ${header.customerId}.
> >
> > I wrote all that free hand just now with out using an IDE so be
> suspicious
> > of the specifics but comfortable with the general approach.
> >
> > On Wed, Sep 28, 2016 at 11:35 PM, Shabin5785 &lt;
>
> > shabin@
>
> > &gt; wrote:
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.
> com/Parse-Json-inside-RecipientList-tp5788162p5788225.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Parse Json inside RecipientList

Posted by Shabin5785 <sh...@outlook.com>.
OK got it.. I added it as a property on exchange.



Ranx wrote
> There are a number of ways but perhaps the simplest would be something
> like:
> 
> .setHeader("customerId","${body.id}")
> 
> right before the marshaling.  Then in the http statement where the ID goes
> you can fish it out of the header with something like
> ${header.customerId}.
> 
> I wrote all that free hand just now with out using an IDE so be suspicious
> of the specifics but comfortable with the general approach.
> 
> On Wed, Sep 28, 2016 at 11:35 PM, Shabin5785 &lt;

> shabin@

> &gt; wrote:





--
View this message in context: http://camel.465427.n5.nabble.com/Parse-Json-inside-RecipientList-tp5788162p5788225.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Parse Json inside RecipientList

Posted by Brad Johnson <br...@mediadriver.com>.
There are a number of ways but perhaps the simplest would be something like:

.setHeader("customerId","${body.id}")

right before the marshaling.  Then in the http statement where the ID goes
you can fish it out of the header with something like ${header.customerId}.

I wrote all that free hand just now with out using an IDE so be suspicious
of the specifics but comfortable with the general approach.

On Wed, Sep 28, 2016 at 11:35 PM, Shabin5785 <sh...@outlook.com> wrote:

> sparekh wrote
> > If the jms message is a serialized POJO then you can use the Camel OGNL
> > expression to grab the id via body.id before marshalling it to JSON.
> >
> > Thanks,
> > sparekh
>
> Message is a serialized pojo. If i extract the id before marshalling, how
> can i hand it over to the next stage?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.
> com/Parse-Json-inside-RecipientList-tp5788162p5788181.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Parse Json inside RecipientList

Posted by Shabin5785 <sh...@outlook.com>.
sparekh wrote
> If the jms message is a serialized POJO then you can use the Camel OGNL
> expression to grab the id via body.id before marshalling it to JSON.
> 
> Thanks,
> sparekh

Message is a serialized pojo. If i extract the id before marshalling, how
can i hand it over to the next stage?



--
View this message in context: http://camel.465427.n5.nabble.com/Parse-Json-inside-RecipientList-tp5788162p5788181.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Parse Json inside RecipientList

Posted by sparekh <sp...@oneweb.net>.
How about a custom processor that parses the JSON string (via GSON) into a
JSON object and sets up the HTTP request and headers. I don't believe you
can use JsonPath to extract values, just choice or filter them.

The custom processor would translate String -> JSON Object, extract the id
from JSON object, set up CONTENT_TYPE, METHOD AND URI Headers, remove non
HTTP headers. If you set the Exchange.HTTP_URI header you can remove the
recipientList and have a to endpoint with a dummy value
(.to("https4://dummy"))

If the jms message is a serialized POJO then you can use the Camel OGNL
expression to grab the idea via body.id

Thanks,
sparekh



--
View this message in context: http://camel.465427.n5.nabble.com/Parse-Json-inside-RecipientList-tp5788162p5788164.html
Sent from the Camel - Users mailing list archive at Nabble.com.