I'm probably overlooking something simple, but how would one go about converting an entire msg object to JSON. In my specific use case, I will be packaging that data into a payload to be sent over a TCP connection between Node-RED instances.
My specific use case at this time is to get messages from distributed instances to a firewalled instance that can talk to an SMTP relay for notifications. I have that working wonderfully using change nodes to explicitly package the traffic I'm wanting to send, with a corresponding unpack operation on the other end, but I'd like to do it a lot more "generically" in the future.
You will need to be aware of what is in the content of the msg object. Some things cannot be serialised (e.g. functions).
Then you can use a function node with JavaScript standard JSON object which has a serialise capability. This turns a JavaScript object into a string which you can pass and then parse at the other end.
JSON.parse()
reverses the serialise.
1 Like
Worked beautifully. In case someone stumbles across this, my flow was as follows:
Outbound: A function node with
var payload = JSON.stringify(msg);
node.send({payload});
going into a TCP out node set to connect to a specific port and close connection after each message.
Inbound: A TCP In node set to listen on the same specified port and output a single string, which then goes to a function node containing:
msg = JSON.parse(msg.payload);
return msg;
Many thanks to TotallyInformation for answering in a way that put me on the right path but was just far enough from hand-holding to make me have to bash my head against it to learn a bit.
3 Likes