# Read payload from a specific msg.topic

**URL:** <https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002>\
**Category:** Developing Nodes\
**Created:** [20 May 2021 20:08 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002 "2021-05-20T20:08:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SwatCat](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SwatCat](https://discourse.nodered.org/u/SwatCat)\
**Post date:** [20 May 2021 20:08 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002/1 "2021-05-20T20:08:47Z")

</div>

![Image2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db58e938ac2dd4928bd3658315e9a1840b559f2c.jpeg)

Hello, i have a long waiting question.

If i have many msg.topics with msg.payload each, how cand i read (with a function block) a msg.payload for a specific msg.topic??

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 May 2021 20:21 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002/2 "2021-05-20T20:21:15Z")

</div>

Use a join node.  
you will find multiple examples here  
[https://discourse.nodered.org/search?q=join%20messages](https://discourse.nodered.org/search?q=join%20messages)

Or save inputs to context storage, then you can access anytime  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![SwatCat](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SwatCat](https://discourse.nodered.org/u/SwatCat)\
**Post date:** [20 May 2021 20:37 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002/3 "2021-05-20T20:37:55Z")

</div>

![Image3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8fd49ec84333694fff6a72f76ea9e8f7339dcab3.jpeg)  
i can use SWITCH, but i need it in a function to use them in some conditions.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 May 2021 20:39 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002/4 "2021-05-20T20:39:21Z")

</div>

If you are just trying to route the message down different wires dependent on the topic then use a Switch node, not a function node.

If you do want to do it in a Function node then you can use a javascript switch statement to test the topic and then, for example, to send it to output 2 use  
`node.send([null, msg, null])`  
or you can do something clever with a lookup array containing all the topics and find the topic in the array and use the index to build the array to be returned.

---

<div class="post-metadata">

**Author:** ![SwatCat](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SwatCat](https://discourse.nodered.org/u/SwatCat)\
**Post date:** [20 May 2021 21:00 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002/5 "2021-05-20T21:00:10Z")

</div>

![Image4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/29c1de4712175591749406493d75d1bd1eff632a.jpeg)  
so.... an IF condition for the msg.topic it will be enough? other easy way?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 May 2021 21:08 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002/6 "2021-05-20T21:08:29Z")

</div>

You don't need to use a new message

```auto
if (msg.topic == 1) {
 return [msg]
}

if (msg.topic == 2) {
 return [null, msg]
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 July 2021 21:08 UTC](https://discourse.nodered.org/t/read-payload-from-a-specific-msg-topic/46002/7 "2021-07-19T21:08:59Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
