JSON global variables in the CHANGE node

Is there a smarter way to prepare this JSON?

I would like all this to be in a single node CHANGE ?

image

image

[{"id":"effa65099dd2a525","type":"function","z":"ea86445f05051d5d","name":"prep_MQTT_Conn","func":"\nconst ip = msg.payload.ip\nconst porta = msg.payload.porta\nconst usuario = msg.payload.usuario\nconst senha = msg.payload.senha\nconst nomeBD = msg.payload.nomeBD\n\nmsg.server = {\n    \"host\": ip,\n    \"port\": porta,\n    \"username\": usuario,\n    \"password\": senha,\n    \"db\": nomeBD\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":280,"wires":[["88e81ef0f7ecf365"]]},{"id":"8cc0b55b7d0743c0","type":"change","z":"ea86445f05051d5d","name":"copy flow/global to msg","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.ip","pt":"msg","to":"IP_vGlobalBD","tot":"global"},{"t":"set","p":"payload.porta","pt":"msg","to":"Porta_vGlobalBD","tot":"global"},{"t":"set","p":"payload.usuario","pt":"msg","to":"Usuario_vGlobalBD","tot":"global"},{"t":"set","p":"payload.senha","pt":"msg","to":"Senha_vGlobalBD","tot":"global"},{"t":"set","p":"payload.nomeBD","pt":"msg","to":"NomeBD_vGlobalBD","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":220,"wires":[["effa65099dd2a525"]]}]

It's just that I'm trying to get good at flow language. I want to avoid functions as much as possible.

1 Like

You can use the JSONata expression type with the $globalContext() function to look something up.

Here's a Change node that creates msg.server with just the host property set - you should be able to add all the others to the expression.

[{"id":"eb96b8b79efaea17","type":"change","z":"c3d095d322ddcf1d","name":"","rules":[{"t":"set","p":"server","pt":"msg","to":"{ 'host': $globalContext('IP_vGlobalBD')}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":760,"wires":[["c8b1a934e6529a99"]]}]

The expression is:

{
   'host': $globalContext('IP_vGlobalBD')
}
1 Like

I am very happy that the founder and creator of node-red responded to my topic.

Congratulations the tool you created is excellent, and I love using it. I'm a big admirer of your work.

Thanks for answering my question. I'm already putting it into practice.v

2 Likes

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