Create mapping template node

Hi experts,

I'm quite new to NodeRed development. I try to implement an integration for a Rest API that requires a specific json payload. I would like to create my own node that allows to map certain fields in that json structure. Right now I place a function node in front of the POST request where I map the fields and create the json. This works fine and is very flexible. But I would like to give the user more convenient functions (users dont know the json structure).

It would be great if I could create a node that has already the json fragment as a template and allows the user to map field from msg. of even better from jsonata espression

Any suggestion would be greatly applreciated.

Thanks in advance,

Marco

I have something similar here - not sure if it's close enough - node-red-contrib-remap (node) - Node-RED

2 Likes

Maybe a subflow is the sweet-spot between a function node and dedicated node?

https://nodered.org/docs/user-guide/editor/workspace/subflows

You can control the basic input values. In the Subflow, you can request the api and prepare the respond for the users.

grafik

[{"id":"e0d28b6139c9b59a","type":"subflow","name":"api","info":"Hi there!\r\nThis is a subflow for the node-red forum.\r\n\r\n`\r\n{\r\n\"payload\": \r\n{\"a\":1,\"b\":2,\"c\":3,\"input_a\":1713959832670,\"input_b\":4,\"input_c\":true}\r\n}\r\n`","category":"","in":[{"x":60,"y":100,"wires":[{"id":"09c8a06e9f8f0332"}]}],"out":[{"x":600,"y":100,"wires":[{"id":"09c8a06e9f8f0332","port":0}]}],"env":[{"name":"input_a","type":"str","value":""},{"name":"input_b","type":"num","value":"","ui":{"type":"spinner","opts":{"min":0,"max":10}}},{"name":"input_c","type":"bool","value":"true","ui":{"type":"checkbox"}}],"meta":{},"color":"#C7E9C0","icon":"font-awesome/fa-magic","status":{"x":600,"y":220,"wires":[{"id":"4d4cfa2fee51f838","port":0}]}},{"id":"09c8a06e9f8f0332","type":"function","z":"e0d28b6139c9b59a","name":"function 3","func":"msg.payload = {\n    \"a\" : 1,\n    \"b\" : 2,\n    \"c\" : 3,\n    \"input_a\" : msg.payload.input_a || env.get(\"input_a\"),\n    \"input_b\": msg.payload.input_b || env.get(\"input_b\"),\n    \"input_c\": msg.payload.input_c || env.get(\"input_c\"),\n}\n\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":100,"wires":[["4d4cfa2fee51f838"]]},{"id":"4d4cfa2fee51f838","type":"change","z":"e0d28b6139c9b59a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.input_a","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":220,"wires":[[]]},{"id":"eb7734c464f808a6","type":"subflow:e0d28b6139c9b59a","z":"94c514c2ac360b08","name":"","env":[{"name":"input_b","value":"4","type":"num"}],"x":430,"y":560,"wires":[["4059ea6cdfd75c07"]]},{"id":"9f13857bea64a70f","type":"inject","z":"94c514c2ac360b08","name":"","props":[{"p":"payload.input_a","v":"","vt":"date"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":560,"wires":[["eb7734c464f808a6"]]},{"id":"4059ea6cdfd75c07","type":"debug","z":"94c514c2ac360b08","name":"debug 21","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":560,"wires":[]}]
1 Like

Thanks you very much for your suggestions! I will look into those.