# Function node for specific message payload objects

**URL:** <https://discourse.nodered.org/t/function-node-for-specific-message-payload-objects/57064>\
**Category:** General\
**Created:** [22 January 2022 20:06 UTC](https://discourse.nodered.org/t/function-node-for-specific-message-payload-objects/57064 "2022-01-22T20:06:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilsonwheels](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/neilsonwheels/32/52311_2.png) [@neilsonwheels](https://discourse.nodered.org/u/neilsonwheels)\
**Post date:** [22 January 2022 20:06 UTC](https://discourse.nodered.org/t/function-node-for-specific-message-payload-objects/57064/1 "2022-01-22T20:06:06Z")

</div>

I have node that produces a msg.payload object with two contents:

activity  
status

I need to continue the flow only when 'activity' = 48481328 and 'status' = 2.

I'm new to node red and struggling to find an example of how to achieve this. I assume the Function node will do it, I just need some help to write it.

TIA

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 January 2022 20:23 UTC](https://discourse.nodered.org/t/function-node-for-specific-message-payload-objects/57064/2 "2022-01-22T20:23:09Z")

</div>

You can do it with a function node, 2 switch nodes in series and probably with a change node using JSONata. 😀 I may have missed some options too.

Easiest with a function node though probably.

```auto
const activity = msg.payload.activity
const status = msg.payload.status

if ( activity === '48481328' && status === 2 ) return msg

// You must make sure you remove the default `return msg`
//return msg

```

Note two things:

1. I've assumed that the status is numeric, but activity is a numeric string. Make sure you get this right.

2. I've used a positive test. This is the recommended best practice because positive tests are generally easier to comprehend. However, they can sometimes result in more long-winded code. In this case, the impact is that you MUST remove the default `return msg` at the end of the function code.

With a negative test, the code would look like:

```auto
const activity = msg.payload.activity
const status = msg.payload.status

// use an OR and return nothing
if ( activity !== '48481328' || status !== 2 ) return

return msg

```

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [22 January 2022 21:09 UTC](https://discourse.nodered.org/t/function-node-for-specific-message-payload-objects/57064/3 "2022-01-22T21:09:12Z")

</div>

It is easier to use a switch node for filtering purposes:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/5593b592ba9a7e1df6ddfff83db32c0195183de0.png)

You can add false as output as well.

I guess it is an output of the harmony node with a specific activity, 😉

If the numbers are strings - then just embed the numbers in quotation marks.

so instead of

```auto
payload = {"activity":48481328, "status":2 }

```

use

```auto
payload = {"activity":"48481328", "status":"2" }

```

or a combination of string/number

```auto
payload = {"activity":"48481328", "status":2 }

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 January 2022 22:09 UTC](https://discourse.nodered.org/t/function-node-for-specific-message-payload-objects/57064/4 "2022-01-22T22:09:04Z")

</div>

Or just use two switch nodes in sequence - 1st one passes if msg.payload.activity == 48481328, 2nd one if msg.payload.status == 2

---

<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:** [23 March 2022 22:09 UTC](https://discourse.nodered.org/t/function-node-for-specific-message-payload-objects/57064/5 "2022-03-23T22:09:20Z")

</div>

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