You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by solomon <au...@gmail.com> on 2017/06/06 12:20:43 UTC

How to Extract json key, value from MQTT payload

Hi,

I have created a camel route of mqtt -> jdbc which pushes the data in json
format into MQTT topic, now I want to extract the json key and value so that
I can insert it into jdbc database.

sample json payload : { "id": 1, "name": "Test" }

My Camel route looks like this :

        from("mqtt:bar?subscribeTopicName=test&host=tcp://localhost:1883")
        .process(new MyProcessor()) // Extract json data here
        .to("jdbc:myDataSource?resetAutoCommit=false"); 

Can any one help me on how to get the values.

Thanks



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-Extract-json-key-value-from-MQTT-payload-tp5801958.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to Extract json key, value from MQTT payload

Posted by Owain McGuire <ow...@integration.technology>.
Try unmarshal from json to pojo using http://camel.apache.org/json.html <http://camel.apache.org/json.html>



> On 6 Jun 2017, at 13:20, solomon <au...@gmail.com> wrote:
> 
> Hi,
> 
> I have created a camel route of mqtt -> jdbc which pushes the data in json
> format into MQTT topic, now I want to extract the json key and value so that
> I can insert it into jdbc database.
> 
> sample json payload : { "id": 1, "name": "Test" }
> 
> My Camel route looks like this :
> 
>        from("mqtt:bar?subscribeTopicName=test&host=tcp://localhost:1883")
>        .process(new MyProcessor()) // Extract json data here
>        .to("jdbc:myDataSource?resetAutoCommit=false"); 
> 
> Can any one help me on how to get the values.
> 
> Thanks
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-Extract-json-key-value-from-MQTT-payload-tp5801958.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: How to Extract json key, value from MQTT payload

Posted by Zoran Regvart <zo...@regvart.com>.
Hi,
yeah, sorry, I made a mistake, to use the named parameters the syntax
is :#id, not :?id, you can see examples in the JDBC component
documentation[1]

On Wed, Jun 7, 2017 at 11:31 AM, solomon <au...@gmail.com> wrote:
> I tried your sugession, now I can parse the json object. However I could not
> able to get the values in
> .setBody("INSERT INTO MY_TABLE (ID, NAME) VALUES (:?id, :?name)")
>
> the output will be like this : INSERT INTO MY_TABLE (ID, NAME) VALUES (:?id,
> :?name)

zoran

[1] https://camel.apache.org/sql-component.html#SQLComponent-Usingnamedparameters
-- 
Zoran Regvart

Re: How to Extract json key, value from MQTT payload

Posted by solomon <au...@gmail.com>.
Hi,

Thanks for your reply.

I tried your sugession, now I can parse the json object. However I could not
able to get the values in 
.setBody("INSERT INTO MY_TABLE (ID, NAME) VALUES (:?id, :?name)")

the output will be like this : INSERT INTO MY_TABLE (ID, NAME) VALUES (:?id,
:?name)

what am I missing?



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-Extract-json-key-value-from-MQTT-payload-tp5801958p5802254.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to Extract json key, value from MQTT payload

Posted by Zoran Regvart <zo...@regvart.com>.
Hi,
so typically you would use message headers and named parameters in
your SQL, something like

.setBody("INSERT INTO MY_TABLE (ID, NAME) VALUES (:?id, :?name)")
.to("jdbc:myDataSource")

and your processor would need to set those `id` and `name` headers on
the incoming message.

You could also use JSONPath support to set those headers, of the top
of my head, something like:

.setHeader('id', new JsonPathExpression("$.id"))
.setHeader('name', new JsonPathExpression("$.name"))
.setBody("INSERT INTO MY_TABLE (ID, NAME) VALUES (:?id, :?name)")
.to("jdbc:myDataSource")

zoran

On Tue, Jun 6, 2017 at 2:20 PM, solomon <au...@gmail.com> wrote:
> Hi,
>
> I have created a camel route of mqtt -> jdbc which pushes the data in json
> format into MQTT topic, now I want to extract the json key and value so that
> I can insert it into jdbc database.
>
> sample json payload : { "id": 1, "name": "Test" }
>
> My Camel route looks like this :
>
>         from("mqtt:bar?subscribeTopicName=test&host=tcp://localhost:1883")
>         .process(new MyProcessor()) // Extract json data here
>         .to("jdbc:myDataSource?resetAutoCommit=false");
>
> Can any one help me on how to get the values.
>
> Thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-Extract-json-key-value-from-MQTT-payload-tp5801958.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Zoran Regvart

RE: How to Extract json key, value from MQTT payload

Posted by Steve Huston <sh...@riverace.com>.
That depends totally on what MyProcessor does with the data and where it puts it.

> -----Original Message-----
> From: solomon [mailto:austin.solomon007@gmail.com]
> Sent: Tuesday, June 06, 2017 8:21 AM
> To: users@camel.apache.org
> Subject: How to Extract json key, value from MQTT payload
> 
> Hi,
> 
> I have created a camel route of mqtt -> jdbc which pushes the data in json
> format into MQTT topic, now I want to extract the json key and value so that
> I can insert it into jdbc database.
> 
> sample json payload : { "id": 1, "name": "Test" }
> 
> My Camel route looks like this :
> 
>         from("mqtt:bar?subscribeTopicName=test&host=tcp://localhost:1883")
>         .process(new MyProcessor()) // Extract json data here
>         .to("jdbc:myDataSource?resetAutoCommit=false");
> 
> Can any one help me on how to get the values.
> 
> Thanks
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-
> Extract-json-key-value-from-MQTT-payload-tp5801958.html
> Sent from the Camel - Users mailing list archive at Nabble.com.