# Using data directly from the JSON output of MQTT

**URL:** <https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024>\
**Category:** General\
**Created:** [22 October 2019 16:03 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024 "2019-10-22T16:03:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [22 October 2019 16:03 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024/1 "2019-10-22T16:03:48Z")

</div>

I have a working flow below, but I want to understand how to simplify it so my flows are cleaner (and learn something new). I was thinking a function node could do all of it, and would appreciate any pointers.

The input is a zigbee door sensor which output the JSON string over MQTT.  
When the door is open (contact = false) I want the message to be passed onto a trigger node.  
When the door is closed (contact = true) I want the message blocked.

Current flow and debugs:

 ![01%20Zigbee2MQTT%20flow%20door%20sensor](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f68f9e2520f9d89e971783faa260a429cd92a195.png)

Change node:  
 ![02%20change%20node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/cd7cf1d110c22380cafac08363f13b4460cf468f.png)

Switch node:  
 ![03%20switch%20node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3e162f902471febee1794fc3babb4cdedd18da03.png)

My basic thoughts of a function node would be:

```auto
var doorstate = msg.payload.contact; //read contact state into a variable

if (doorstate = false) // false means door is open
msg.payload = 1; //any value works when sent to the trigger node

return msg;

```

I'm not sure how to read the payload from the MQTT node.  
Also I 'think' a payload may still get passed through to the trigger node like this.

Thanks

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [22 October 2019 16:26 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024/3 "2019-10-22T16:26:35Z")

</div>

You can set the mqtt node to ouput json directly, saves 1 node.

In the change node example, you change `false` to `"false"`, could you explain why this is needed ?

The easiest would be to use a switch node and configure it like:

 ![09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e38fa67c2ea90a776162145851033ec333e28c76.jpeg)

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [22 October 2019 16:40 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024/4 "2019-10-22T16:40:02Z")

</div>

Hi @bakman2

It's because the switch node does not have a "== boolean true" option, so it did not work.  
So I am changing it from a "booleon true" to a string which is "true".

Probably totally wrong logic on my part 🙂

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [22 October 2019 16:41 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024/5 "2019-10-22T16:41:26Z")

</div>

The `is true` and `is false` is the boolean test in the switch node.

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [22 October 2019 16:46 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024/6 "2019-10-22T16:46:23Z")

</div>

Of course! That makes sense thanks.  
I believe I still currently need the change node to set the payload to "payload.contact".

Any tips how to read the JSON message from the MQTT node to get the contact state?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [22 October 2019 16:48 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024/7 "2019-10-22T16:48:35Z")

</div>

You don't need the change node - if the mqtt node outputs the example above.

mqtt (set the output to `a parsed JSON object`) -\> switch node (configure it as shown in the reply above) -\> trigger node

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [22 October 2019 17:28 UTC](https://discourse.nodered.org/t/using-data-directly-from-the-json-output-of-mqtt/17024/8 "2019-10-22T17:28:19Z")

</div>

Excellent - that all works with only the MQTT node \> switch node \> trigger node.

I really ought to look at all the node options in the future.

Thanks very much @bakman2 👍
