Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 10.1 KB

JSONata_PRACTICAL.md

File metadata and controls

27 lines (20 loc) · 10.1 KB

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


Practical - replace function nodes with equivalent functionality

Import the flow file provided and create equivalent functionality of the function nodes using only the nodes in the standard pallet except the function node.

[{"id":"3e47b1d1.73c8b6","type":"inject","z":"4c7ff9bd.c8321","name":"solution4","topic":"","payload":"[{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acmeA.inc\"},{\"name\":\"jack\",\"age\":35,\"email\":\"jack@acmeB.inc\"},{\"name\":\"jill\",\"age\":30,\"email\":\"jill@acmeC.inc\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"x":320,"y":820,"wires":[["977592ad.f1fc5"]]},{"id":"9d4099fe.3d394","type":"inject","z":"4c7ff9bd.c8321","name":"test1","topic":"","payload":"{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":330,"y":60,"wires":[["76ec5237.b7a9b4"]]},{"id":"76ec5237.b7a9b4","type":"function","z":"4c7ff9bd.c8321","name":"function 1","func":"msg.payload = msg.payload.name;\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":60,"wires":[["9c9b63a3.d003c"]]},{"id":"9c9b63a3.d003c","type":"debug","z":"4c7ff9bd.c8321","name":"","active":true,"console":"false","complete":"false","x":970,"y":60,"wires":[]},{"id":"805b5e22.e43be8","type":"inject","z":"4c7ff9bd.c8321","name":"test2","topic":"","payload":"{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":330,"y":160,"wires":[["11ffac2c.f677e4"]]},{"id":"11ffac2c.f677e4","type":"function","z":"4c7ff9bd.c8321","name":"function 2","func":" var output = msg.payload.name + \" is \" + msg.payload.age + \" years old\";\n msg.payload = output;\n return msg;","outputs":1,"noerr":0,"x":540,"y":160,"wires":[["e1a554f0.d9038"]]},{"id":"e1a554f0.d9038","type":"debug","z":"4c7ff9bd.c8321","name":"","active":true,"console":"false","complete":"false","x":970,"y":160,"wires":[]},{"id":"3bd31a1d.2122a6","type":"inject","z":"4c7ff9bd.c8321","name":"test3","topic":"","payload":"[{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"},{\"name\":\"jack\",\"age\":35,\"email\":\"jack@acme.inc\"},{\"name\":\"jill\",\"age\":30,\"email\":\"jill@acme.inc\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"x":330,"y":260,"wires":[["ccf2d195.30d99"]]},{"id":"ccf2d195.30d99","type":"function","z":"4c7ff9bd.c8321","name":"function 3","func":"output = {};\nfor (var i of msg.payload) {\n    output[i.name] = \"Age is \" + i.age + \" and mail address is \" + i.email;\n}\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":260,"wires":[["5dd1b411.c918d4"]]},{"id":"5dd1b411.c918d4","type":"debug","z":"4c7ff9bd.c8321","name":"","active":true,"console":"false","complete":"false","x":970,"y":260,"wires":[]},{"id":"994dc2a6.2f804","type":"inject","z":"4c7ff9bd.c8321","name":"test4","topic":"","payload":"[{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acmeA.inc\"},{\"name\":\"jack\",\"age\":35,\"email\":\"jack@acmeB.inc\"},{\"name\":\"jill\",\"age\":30,\"email\":\"jill@acmeC.inc\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"x":330,"y":360,"wires":[["73151d54.b0cc44"]]},{"id":"73151d54.b0cc44","type":"function","z":"4c7ff9bd.c8321","name":"function 4","func":"output = \"\";\nfirst = true;\nfor (var i of msg.payload) {\n    if (!first) {\n        output += ', ';\n    } else {\n        first = false;\n    }\n    output += i.name + \" works for \" + i.email.split('@')[1];\n}\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":360,"wires":[["962b6896.34b768"]]},{"id":"962b6896.34b768","type":"debug","z":"4c7ff9bd.c8321","name":"","active":true,"console":"false","complete":"false","x":970,"y":360,"wires":[]},{"id":"f6421195.d5ff9","type":"inject","z":"4c7ff9bd.c8321","name":"solution1","topic":"","payload":"{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":320,"y":520,"wires":[["19465e55.aa2c4a"]]},{"id":"19465e55.aa2c4a","type":"debug","z":"4c7ff9bd.c8321","name":"output1","active":true,"console":"false","complete":"payload","x":960,"y":520,"wires":[]},{"id":"1884d3f3.45c91c","type":"inject","z":"4c7ff9bd.c8321","name":"solution2","topic":"","payload":"{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":320,"y":620,"wires":[["d9beea96.284348"]]},{"id":"d9beea96.284348","type":"debug","z":"4c7ff9bd.c8321","name":"output2","active":true,"console":"false","complete":"payload","x":960,"y":620,"wires":[]},{"id":"a9eec9f7.5f95f8","type":"inject","z":"4c7ff9bd.c8321","name":"solution3","topic":"","payload":"[{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"},{\"name\":\"jack\",\"age\":35,\"email\":\"jack@acme.inc\"},{\"name\":\"jill\",\"age\":30,\"email\":\"jill@acme.inc\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"x":320,"y":720,"wires":[["3f60ab64.35b55c"]]},{"id":"3f60ab64.35b55c","type":"debug","z":"4c7ff9bd.c8321","name":"output3","active":true,"console":"false","complete":"payload","x":960,"y":720,"wires":[]},{"id":"977592ad.f1fc5","type":"debug","z":"4c7ff9bd.c8321","name":"output4","active":true,"console":"false","complete":"payload","x":960,"y":820,"wires":[]},{"id":"924ef82d.87dc18","type":"comment","z":"4c7ff9bd.c8321","name":"Add solution for test 1 here","info":"","x":670,"y":500,"wires":[]},{"id":"b83b42f.2e7724","type":"comment","z":"4c7ff9bd.c8321","name":"Add solution for test 2 here","info":"","x":670,"y":600,"wires":[]},{"id":"a668fec2.8a4ca8","type":"comment","z":"4c7ff9bd.c8321","name":"Add solution for test 3 here","info":"","x":670,"y":700,"wires":[]},{"id":"6c2caba6.da9f34","type":"comment","z":"4c7ff9bd.c8321","name":"Add solution for test 4 here","info":"","x":670,"y":800,"wires":[]},{"id":"2be9e5f0.9f6f42","type":"comment","z":"4c7ff9bd.c8321","name":"Test scenarios","info":"You need to implement the equivalent functionality of the function nodes without using a function node and only using the preinstalled nodes","x":290,"y":20,"wires":[]},{"id":"6a8c9969.4ac07","type":"comment","z":"4c7ff9bd.c8321","name":"ADD YOUR SOLUTIONS HERE","info":"Implement your solutions to the test scenarios.  \n\nsolution 1 should be the solution to test 1, solution 2 for test 2, etc...  \n","x":350,"y":460,"wires":[]}]

Sample solutions

You can compare your solutions with a sample solution :

[{"id":"b46321da.451fd8","type":"inject","z":"4c7ff9bd.c8321","name":"solution1","topic":"","payload":"{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":300,"y":940,"wires":[["e911f31.1da3b1"]]},{"id":"3d41d1f2.fec3c6","type":"debug","z":"4c7ff9bd.c8321","name":"output1","active":true,"console":"false","complete":"false","x":950,"y":940,"wires":[]},{"id":"f150cd31.14b7c8","type":"inject","z":"4c7ff9bd.c8321","name":"solution2","topic":"","payload":"{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"}","payloadType":"json","repeat":"","crontab":"","once":false,"x":300,"y":1040,"wires":[["9a69c390.2e669"]]},{"id":"c956d821.5006f","type":"debug","z":"4c7ff9bd.c8321","name":"output2","active":true,"console":"false","complete":"payload","x":940,"y":1040,"wires":[]},{"id":"60d11d6e.e4bddc","type":"inject","z":"4c7ff9bd.c8321","name":"solution3","topic":"","payload":"[{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acme.inc\"},{\"name\":\"jack\",\"age\":35,\"email\":\"jack@acme.inc\"},{\"name\":\"jill\",\"age\":30,\"email\":\"jill@acme.inc\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"x":300,"y":1140,"wires":[["24f8364a.8464c2"]]},{"id":"a6afb94b.f59a8","type":"debug","z":"4c7ff9bd.c8321","name":"output3","active":true,"console":"false","complete":"payload","x":940,"y":1140,"wires":[]},{"id":"18335154.63fd17","type":"inject","z":"4c7ff9bd.c8321","name":"solution4","topic":"","payload":"[{\"name\":\"fred\",\"age\":25,\"email\":\"fred@acmeA.inc\"},{\"name\":\"jack\",\"age\":35,\"email\":\"jack@acmeB.inc\"},{\"name\":\"jill\",\"age\":30,\"email\":\"jill@acmeC.inc\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"x":300,"y":1240,"wires":[["c2c975ee.6dd948"]]},{"id":"4db7b9c1.b7b2b","type":"debug","z":"4c7ff9bd.c8321","name":"output4","active":true,"console":"false","complete":"payload","x":940,"y":1240,"wires":[]},{"id":"e911f31.1da3b1","type":"change","z":"4c7ff9bd.c8321","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.name","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":940,"wires":[["3d41d1f2.fec3c6"]]},{"id":"9a69c390.2e669","type":"template","z":"4c7ff9bd.c8321","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.name}} is {{payload.age}} years old","output":"str","x":590,"y":1040,"wires":[["c956d821.5006f"]]},{"id":"24f8364a.8464c2","type":"change","z":"4c7ff9bd.c8321","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"msg.payload{name : \"Age is \" & age & \" and mail address is \" & email}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":1140,"wires":[["a6afb94b.f59a8"]]},{"id":"c2c975ee.6dd948","type":"change","z":"4c7ff9bd.c8321","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$join(msg.payload.(name & \" works for \" & $replace(email, /\\w*@(\\w*)/, \"$1\") ), ', ')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":1240,"wires":[["4db7b9c1.b7b2b"]]}]

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