Search Through Array with Switch Node Help

Hi,

I am sending a photo to Imagga for identification and I'm getting back a large payload with a load of classifications (usually over 40).

I would like to set up a switch node so that if any of the classifications are bird then some processing is done, and if not then the response is dropped or an error is shown.

This is part of the payload:

payload: object
     result: object
          tags: array[62]
            [0 … 9]
               0: object
                 confidence: 100
               tag: object
               1: object
                   confidence: 74.7238693237305
                   tag: object
                         en: "bird"
             2: object
             3: object
             4: object
             5: object
             6: object
             7: object
             8: object
             9: object
       [10 … 19]
       [20 … 29]
       [30 … 39]
       [40 … 49]
       [50 … 59]
       [60 … 61]

I have a tried a switch with a "matches regex" for bird but when I can only set the property as a single entry in the payload, not everything from "Result" down.

Is there any way to recurse through the entries looking for a string and then choose a path based on the result?

Thank again for all your help!

Probably possible with JSONata in a change node

In a function node...

var tags = msg.payload.result.tags;
var birds = tags.filter(e => e.tag && e.tag.en == "bird");
if(!birds || birds.length < 1) {
  node.error("no birds", msg);
  return null;//halt flow
}
msg.payload = birds;
return msg; //return msg to next node
1 Like

Absolutely perfect!

Thanks so much for the very fast and accurate response! Your code worked a treat!

Node Red is an amazing tool and the support in the forum is second to none.

Thanks again - keep up the great work.

As an afterthought, do you know when Node Red V2 is being released? I can't wait to see the new version!

Hi, you could use a JSONATA expression in the switch node.
e.g.

[{"id":"1bb7d78f.756dd","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"result\":{\"tags\":[{\"confidence\":100,\"tag\":{\"en\":\"bat\"}},{\"confidence\":74.7238693237305,\"tag\":{\"en\":\"bird\"}}]}}","payloadType":"json","x":120,"y":1840,"wires":[["4d3b8a44.88aadc"]]},{"id":"4d3b8a44.88aadc","type":"switch","z":"c74669a0.6a34f8","name":"","property":"$count(payload.result.tags[tag.en = \"bird\"])","propertyType":"jsonata","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":280,"y":1860,"wires":[["1cd0bf9c.c79d38"]]},{"id":"1cd0bf9c.c79d38","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":1860,"wires":[]}]
$count(payload.result.tags[tag.en = "bird"])

you could also filter the payload to return only array elements that contain "bird", then use switch node to only pass if payload is not empty.
e.g.

[{"id":"1bb7d78f.756dd","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"result\":{\"tags\":[{\"confidence\":100,\"tag\":{\"en\":\"bat\"}},{\"confidence\":74.7238693237305,\"tag\":{\"en\":\"bird\"}}]}}","payloadType":"json","x":120,"y":1840,"wires":[["cf950648.c9b42"]]},{"id":"cf950648.c9b42","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.result.tags[tag.en = \"bird\"]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1840,"wires":[["4d3b8a44.88aadc"]]},{"id":"4d3b8a44.88aadc","type":"switch","z":"c74669a0.6a34f8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":480,"y":1840,"wires":[["1cd0bf9c.c79d38"]]},{"id":"1cd0bf9c.c79d38","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1840,"wires":[]}]
1 Like

That is a very slick solution, thanks! I appreciate the example too - that really helps me.

I am really enjoying learning all these tricks.

Cheers!!

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