Simple payload filter using switch node

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:

{
    "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):

[{"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"]]}]

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

So your switch node would be correct as follows:

image

[
    {
        "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"
            ]
        ]
    }
]

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!

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