Quick links : Home - Part 1 - Part 2 - Part 3 - Part 4 - Part 5
Part 3 - Intro to JSONata - Data transformation - JSONata in switch node - Practical work
JSONata is a JSON query and transformation language that is built into Node-RED. It provides a lightweight, but powerful way to query and transform JSON data.
Both the Switch and Change nodes have options to use JSONata expressions within the node and they also provide a JSONata editor, where test data can be entered allowing live results to be displayed as the JSONata expression is edited.
Full documentation can be found on the JSONata site.
This section is not going to provide a full tutorial on JSONata, but give an overview of how to use JSONata within Node-RED and show how powerful it can be.
Many developers use the Function node to perform simple operations, many of which can be performed using JSONata. The following flow demonstrates the overhead of using the function node, as it uses a sandbox to execute the configured JavaScript code:
[{"id":"21a32490.2dc49c","type":"inject","z":"3af82246.3634ae","name":"Function start","topic":"","payload":"100000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":600,"wires":[["b11bbfc7.8a0488"]]},{"id":"31081879.f3c048","type":"inject","z":"3af82246.3634ae","name":"JSONata start","topic":"","payload":"100000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":740,"wires":[["445e4104.53b0f"]]},{"id":"b11bbfc7.8a0488","type":"change","z":"3af82246.3634ae","name":"msg.start timestamp","rules":[{"t":"set","p":"start","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":600,"wires":[["dfe8cb03.eff7e"]]},{"id":"445e4104.53b0f","type":"change","z":"3af82246.3634ae","name":"msg.start timestamp","rules":[{"t":"set","p":"start","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":740,"wires":[["eb22a08c.1e7b08"]]},{"id":"dfe8cb03.eff7e","type":"switch","z":"3af82246.3634ae","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":790,"y":600,"wires":[["13c11387.31abac"],["40a3686b.d68098"]]},{"id":"13c11387.31abac","type":"function","z":"3af82246.3634ae","name":"decrement payload","func":"msg.payload--;\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":520,"wires":[["dfe8cb03.eff7e"]]},{"id":"40a3686b.d68098","type":"change","z":"3af82246.3634ae","name":"","rules":[{"t":"set","p":"duration","pt":"msg","to":"($millis() - start)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1070,"y":600,"wires":[["a391eb34.6df3e8"]]},{"id":"a391eb34.6df3e8","type":"debug","z":"3af82246.3634ae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1230,"y":600,"wires":[]},{"id":"eb22a08c.1e7b08","type":"switch","z":"3af82246.3634ae","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":790,"y":740,"wires":[["2eca5760.edb8b"],["40a3686b.d68098"]]},{"id":"2eca5760.edb8b","type":"change","z":"3af82246.3634ae","name":"decrement payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload -1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":660,"wires":[["eb22a08c.1e7b08"]]},{"id":"9ad24ce3.1b7d8","type":"inject","z":"3af82246.3634ae","name":"Single function start","topic":"","payload":"100000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":840,"wires":[["fa0d54a9.7ad778"]]},{"id":"fa0d54a9.7ad778","type":"change","z":"3af82246.3634ae","name":"msg.start timestamp","rules":[{"t":"set","p":"start","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":840,"wires":[["96856f6d.43e0a8"]]},{"id":"96856f6d.43e0a8","type":"function","z":"3af82246.3634ae","name":"while in function","func":"while (msg.payload > 0) {\n msg.payload--;\n}\nreturn msg;","outputs":1,"noerr":0,"x":780,"y":840,"wires":[["40a3686b.d68098"]]},{"id":"177cc76.ee4b339","type":"inject","z":"3af82246.3634ae","name":"All functions start","topic":"","payload":"100000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":440,"wires":[["8853011.987a"]]},{"id":"8853011.987a","type":"change","z":"3af82246.3634ae","name":"msg.start timestamp","rules":[{"t":"set","p":"start","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":440,"wires":[["7b68a259.79f834"]]},{"id":"3f473f3b.df4f2","type":"function","z":"3af82246.3634ae","name":"decrement payload","func":"msg.payload--;\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":360,"wires":[["7b68a259.79f834"]]},{"id":"7b68a259.79f834","type":"function","z":"3af82246.3634ae","name":"switch in function","func":"if (msg.payload >= 0) {\n return [msg, null];\n} else {\n return [null, msg];\n}","outputs":2,"noerr":0,"x":770,"y":440,"wires":[["3f473f3b.df4f2"],["40a3686b.d68098"]]},{"id":"94c29257.321f4","type":"comment","z":"3af82246.3634ae","name":"Payload to 100000","info":"","x":250,"y":280,"wires":[]},{"id":"ec6aaaf0.28c6c8","type":"comment","z":"3af82246.3634ae","name":"Record start time","info":"","x":460,"y":280,"wires":[]},{"id":"1d759e79.f4729a","type":"comment","z":"3af82246.3634ae","name":"Countdown to 0","info":"","x":760,"y":280,"wires":[]},{"id":"9c30b875.23262","type":"comment","z":"3af82246.3634ae","name":"Calculate duration","info":"","x":1070,"y":280,"wires":[]},{"id":"7644ce22.596cd8","type":"comment","z":"3af82246.3634ae","name":"Output duration","info":"","x":1260,"y":280,"wires":[]}]
Part 3 - Intro to JSONata - Data transformation - JSONata in switch node - Practical work
Quick links : Home - Part 1 - Part 2 - Part 3 - Part 4 - Part 5