Message Payload - Split

Hello

I'm using the node-red-contrib-tfjs-coco-ssd node for object detection. The msg.payload is an array with objects and other arrays inside and looks like this:

[
{"bbox":[97.20104217529297,303.84454250335693,736.6763687133789,605.1833009719849],
"class":"car",
"score":0.972379744052887},

{"bbox":[1258.2083129882812,170.14039278030396,906.0614013671875,560.3293204307556],
"class":"car",
"score":0.952843189239502},

{"bbox":[737.4549865722656,286.9120788574219,512.7299499511719,357.6991367340088],
"class":"person",
"score":0.9017499685287476},

{"bbox":[1959.5710754394531,273.81558179855347,494.97406005859375,259.57021951675415],
"class":"person",
"score":0.8203449249267578},

{"bbox":[2402.5328063964844,280.58627128601074,150.6939697265625,158.15484523773193],
"class":"car",
"score":0.6040482521057129}
]

For further operations I would like to pass (with the node UPD OUT) a msg.payload as a STRING which only delivers the information how often a CAR and how often a PERSON was counted.

For example: [ { "car" : 3, "person" : 2 } ]

I've tried a lot with SPLIT/CHANGE /JOIN notes, but without success so far. Therefore I hope one of you can give me a help.

Thank you, Best regards

Try this Jsonata expression

[{"id":"3f35d274.99189e","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":1820,"wires":[]},{"id":"ac8a2c74.bf42","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"cars\":$count(payload[class=\"car\"]\t),\"person\":$count(payload[class=\"person\"]\t)}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":2080,"wires":[["3f35d274.99189e"]]},{"id":"40b82525.d4037c","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[ {\"bbox\":[97.20104217529297,303.84454250335693,736.6763687133789,605.1833009719849], \"class\":\"car\", \"score\":0.972379744052887},  {\"bbox\":[1258.2083129882812,170.14039278030396,906.0614013671875,560.3293204307556], \"class\":\"car\", \"score\":0.952843189239502},  {\"bbox\":[737.4549865722656,286.9120788574219,512.7299499511719,357.6991367340088], \"class\":\"person\", \"score\":0.9017499685287476},  {\"bbox\":[1959.5710754394531,273.81558179855347,494.97406005859375,259.57021951675415], \"class\":\"person\", \"score\":0.8203449249267578},  {\"bbox\":[2402.5328063964844,280.58627128601074,150.6939697265625,158.15484523773193], \"class\":\"car\", \"score\":0.6040482521057129}]","payloadType":"json","x":80,"y":2080,"wires":[["ac8a2c74.bf42"]]}]
{
   "cars":$count(payload[class="car"]),
   "person":$count(payload[class="person"])
}

Welcome to the forum.

If you are a complete beginner then I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Then feed the message through a Split node which will split the array into a sequence of individual messages, one for each element of the array. Feed that into a debug node and make sure that is happening correctly. Now read carefully the page from the node red docs Working with Messages which will show you how to access the individual components of the message.
When you get to that point if you are still stuck then ask again showing us an individual message and tell us what you want to do with it.

Hello, thank you. It works so far, that I get back an OBJECT. But how can i convert it into a string message? Thank you, BR

Have you watched the videos and read the docs I suggested?

Yes, already yesterday. but this was not answering my questions.

And read Working with Documents? If so then show us what you now have and tell us exactly what format you want it.

If you set the debug to show complete message you should see a property called msg.classes that already has the information you want. You can use a change node to move this to msg.payload and then a json node to make it into a string and then send to udp.

1 Like

concat the items in jsonata, or add a json node after and return a string

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