Chercher dans un tableau

Super. I am happy it works. So, explaining:

1- Stores in msg.payload the array with the interesting information.
2- Split each element of the array into a separate message, so we can modify, transform, sort, filter, etc.
3- The sort node has an internal buffer that will collect all individual pieces and will output them (individually) sorted based on a property of the message (gas price in our case).
4- The switch node here is working as a filter. It will pass only the head message, that is the first one from the sequence. As the sequence was sorted in ascendant order we will get only the message that has the lower gas price.

Message sequences is an effective way to transform data without using explicit JavaScript loops.

There is an alternative that is the use of JSONata expressions. It allows to transform complex data sets but comes with a price. JSONata is hard to learn and easy to forget (at least that is my case).

Using JSONata we could get a shorter flow.

[{"id":"4a48be2a.5cbd2","type":"inject","z":"b13d121c.2a569","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1040,"wires":[["7ce4cec1.54f97"]]},{"id":"4fd2eff1.4835c","type":"http request","z":"b13d121c.2a569","name":"http request","method":"GET","ret":"obj","paytoqs":true,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":1040,"wires":[["d7e67d42.11467"]]},{"id":"d7e67d42.11467","type":"change","z":"b13d121c.2a569","name":"","rules":[{"t":"set","p":"sorted","pt":"msg","to":"payload.records^(fields.price_gazole)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":1040,"wires":[["a435a67a.58e988"]]},{"id":"a435a67a.58e988","type":"debug","z":"b13d121c.2a569","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"sorted","targetType":"msg","x":930,"y":1040,"wires":[]},{"id":"eb155cc5.eabcd","type":"comment","z":"b13d121c.2a569","name":"Using JSONata only","info":"","x":210,"y":980,"wires":[]},{"id":"7ce4cec1.54f97","type":"change","z":"b13d121c.2a569","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"dataset\":\"prix-des-carburants-j-7\",\"facet\":\"brand\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":1040,"wires":[["4fd2eff1.4835c"]]}]
1 Like