# Converting \*entire\* msg object to JSON

**URL:** <https://discourse.nodered.org/t/converting-entire-msg-object-to-json/5967>\
**Category:** General\
**Created:** [19 December 2018 15:39 UTC](https://discourse.nodered.org/t/converting-entire-msg-object-to-json/5967 "2018-12-19T15:39:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [19 December 2018 15:39 UTC](https://discourse.nodered.org/t/converting-entire-msg-object-to-json/5967/1 "2018-12-19T15:39:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 December 2018 15:50 UTC](https://discourse.nodered.org/t/converting-entire-msg-object-to-json/5967/2 "2018-12-19T15:50:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [19 December 2018 17:15 UTC](https://discourse.nodered.org/t/converting-entire-msg-object-to-json/5967/3 "2018-12-19T17:15:00Z")

</div>

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](https://discourse.nodered.org/t/converting-entire-msg-object-to-json/5967/2) 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.
