Chercher dans un tableau

bonjour a tous
je cherche a trouver le prix le moins cher et afficher les informations qui vont avec

[{"id":"c2d7159b.44db98","type":"http request","z":"3dafcb6f.c00894","name":"1","method":"GET","ret":"txt","paytoqs":false,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/?dataset=prix-des-carburants-j-7&facet=city&facet=fuel&facet=brand&facet=address&facet=services","tls":"","persist":false,"proxy":"","authType":"","x":270,"y":80,"wires":[["475e4c66.af1754"]]}]

j'ai trouver comment chercher le moins cher avec ça

[{"id":"d2d980d.a38238","type":"inject","z":"3dafcb6f.c00894","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":200,"wires":[["4d7fd001.3647f"]]},{"id":"4d7fd001.3647f","type":"http request","z":"3dafcb6f.c00894","name":"1","method":"GET","ret":"txt","paytoqs":false,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/?dataset=prix-des-carburants-j-7&facet=cp&facet=pop&facet=city&facet=automate_24_24&facet=fuel&facet=shortage&facet=update&facet=services&facet=brand","tls":"","persist":false,"proxy":"","authType":"","x":230,"y":200,"wires":[["96796bac.bf8dc8"]]},{"id":"96796bac.bf8dc8","type":"json","z":"3dafcb6f.c00894","name":"","property":"payload","action":"","pretty":false,"x":350,"y":200,"wires":[["1de7d98e.a43726"]]},{"id":"1de7d98e.a43726","type":"function","z":"3dafcb6f.c00894","name":"var","func":"for (i=0; i<=8; i++) {\nvar  out = msg.payload.records[i].fields.price_gazole;\nnode.send({payload: out});\n}\n","outputs":1,"noerr":0,"x":470,"y":200,"wires":[["dbbcbaa0.68ddd8"]]},{"id":"dbbcbaa0.68ddd8","type":"smooth","z":"3dafcb6f.c00894","name":"","property":"payload","action":"min","count":"9","round":"3","mult":"single","reduce":true,"x":620,"y":200,"wires":[["cd059816.f02aa8"]]},{"id":"cd059816.f02aa8","type":"debug","z":"3dafcb6f.c00894","name":"test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":770,"y":200,"wires":[]}]

mais comment faire pour avoir toutes les information qui vont avec
j'ai fait plein de recherche, plein d'essais mais je ne trouve rien
je suis a la recherche d'idées nouvelles
merci a vous

You may want to try this flow. I will be glad to explain how it works but please confirm it does what you want.

[{"id":"89e74a75.a195d8","type":"inject","z":"b13d121c.2a569","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":640,"wires":[["2b06e9d5.adf616"]]},{"id":"2b06e9d5.adf616","type":"http request","z":"b13d121c.2a569","name":"1","method":"GET","ret":"txt","paytoqs":false,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/?dataset=prix-des-carburants-j-7&facet=cp&facet=pop&facet=city&facet=automate_24_24&facet=fuel&facet=shortage&facet=update&facet=services&facet=brand","tls":"","persist":false,"proxy":"","authType":"","x":390,"y":640,"wires":[["2363d903.e0f3d6"]]},{"id":"2363d903.e0f3d6","type":"json","z":"b13d121c.2a569","name":"","property":"payload","action":"","pretty":false,"x":510,"y":640,"wires":[["f51e604a.4742a"]]},{"id":"f363e1f6.1a465","type":"debug","z":"b13d121c.2a569","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1250,"y":740,"wires":[]},{"id":"f51e604a.4742a","type":"change","z":"b13d121c.2a569","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.records","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":640,"wires":[["5af8c590.f8d80c","4dbfdee1.c2429"]]},{"id":"67cf04c3.fd3dec","type":"sort","z":"b13d121c.2a569","name":"","order":"ascending","as_num":false,"target":"","targetType":"seq","msgKey":"","msgKeyType":"elem","seqKey":"payload.fields.price_gazole","seqKeyType":"msg","x":970,"y":740,"wires":[["680f224.8944cdc"]]},{"id":"5af8c590.f8d80c","type":"debug","z":"b13d121c.2a569","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.fields.price_gazole","targetType":"jsonata","x":900,"y":580,"wires":[]},{"id":"4dbfdee1.c2429","type":"split","z":"b13d121c.2a569","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":830,"y":740,"wires":[["67cf04c3.fd3dec"]]},{"id":"680f224.8944cdc","type":"switch","z":"b13d121c.2a569","name":"","property":"payload","propertyType":"msg","rules":[{"t":"head","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1110,"y":740,"wires":[["f363e1f6.1a465"]]}]

Thank you so so much! I've been looking for this for weeks! It will be wonderfull if you could explain some things, like what is the last "switch node"? What is it for?
It's amazing, thanks again!

1 Like

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

Amazing, everything's clearer now. Thank you, it's really great. Gonna work on it ASAP.
Have a nice weekend!

I feel less lonely... :wink:

GV

2 Likes

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