# Simple payload filter using switch node

**URL:** <https://discourse.nodered.org/t/simple-payload-filter-using-switch-node/59462>\
**Category:** General\
**Created:** [7 March 2022 01:53 UTC](https://discourse.nodered.org/t/simple-payload-filter-using-switch-node/59462 "2022-03-07T01:53:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![telinal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/telinal/32/57974_2.png) [@telinal](https://discourse.nodered.org/u/telinal)\
**Post date:** [7 March 2022 01:53 UTC](https://discourse.nodered.org/t/simple-payload-filter-using-switch-node/59462/1 "2022-03-07T01:53:47Z")

</div>

Hi there,

I have some JSON data which I want to filter using the Switch node, but I am not being succesful. The JSON is this:

```auto
{
    "payload": {
        "deviceAddress": 111,
        "timestamp": "2022-03-06T21:19:17",
        "temperature": 22,
        "sequence": 545455
    }
}

```

The "payload.deviceAddress" can have many different values, and I need to be able to filter only one specific value (specified in the switch node).

Here is the test nodes I have setup (started with a simple switch and played with JSonata also but no luck):

```auto
[{"id":"b7eda5e281bbca49","type":"debug","z":"8f0861c83fb387a2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":240,"wires":[]},{"id":"dbfebcda3dfb4e1d","type":"switch","z":"8f0861c83fb387a2","name":"Filter 999","property":"payload.deviceAddress","propertyType":"jsonata","rules":[{"t":"eq","v":"\"999\"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":320,"wires":[["b7eda5e281bbca49"]]},{"id":"298bbde330e1b5e7","type":"inject","z":"8f0861c83fb387a2","name":"Inject 111","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"payload\":{\"deviceAddress\":111,\"timestamp\":\"2022-03-06T21:19:17\",\"temperature\":22,\"sequence\":545455}}","payloadType":"json","x":160,"y":240,"wires":[["b7eda5e281bbca49"]]},{"id":"73fca0466488bd92","type":"inject","z":"8f0861c83fb387a2","name":"Inject 999","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"payload\":{\"deviceAddress\":999,\"timestamp\":\"2022-03-06T21:19:17\",\"temperature\":22,\"sequence\":545455}}","payloadType":"json","x":160,"y":320,"wires":[["dbfebcda3dfb4e1d"]]}]

```

---

<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:** [7 March 2022 02:11 UTC](https://discourse.nodered.org/t/simple-payload-filter-using-switch-node/59462/2 "2022-03-07T02:11:21Z")

</div>

You did a mistake to inject in a payload a payload object and then you used a string for comparison and switched with JSONATA instead the msg properties.

In real live you have probably to convert a JSON string into a JS object - if you use an inject node automatically a JS Sting will be the output if you define a string.

1. First of all you have to see that you specified a payload object within the payload object,
2. You should filter to msg properties and not a JSONATA comparision
3. You have to filter a number not a string.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14322f9f6d6bca1f6e7508a2f59ab488e7d86506.png)

So your switch node would be correct as follows:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/94c46e85574288489ac97b46488efeb394c9f79b.png)

```auto
[
    {
        "id": "dbfebcda3dfb4e1d",
        "type": "switch",
        "z": "970cf3130bc10c6c",
        "name": "Filter 999",
        "property": "payload.payload.deviceAddress",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "999",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 540,
        "y": 440,
        "wires": [
            [
                "b7eda5e281bbca49"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![telinal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/telinal/32/57974_2.png) [@telinal](https://discourse.nodered.org/u/telinal)\
**Post date:** [7 March 2022 11:01 UTC](https://discourse.nodered.org/t/simple-payload-filter-using-switch-node/59462/3 "2022-03-07T11:01:40Z")

</div>

Thank you, the source of the problem was my incorrect JSON and how I was addressing the objects, I fixed that and now use the msg properties fine to filter!

---

<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:** [21 March 2022 11:02 UTC](https://discourse.nodered.org/t/simple-payload-filter-using-switch-node/59462/4 "2022-03-21T11:02:35Z")

</div>

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