# Msg.payload assumption

**URL:** <https://discourse.nodered.org/t/msg-payload-assumption/15357>\
**Category:** Feature Requests\
**Created:** [10 September 2019 10:55 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357 "2019-09-10T10:55:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 September 2019 10:55 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/1 "2019-09-10T10:55:04Z")

</div>

Many nodes assume the input is `msg.payload`, like the switch node

 ![20](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2a70e595ce82c37a8eda5ecdc3575aa0c7c71811.jpeg)

Can one circumvent the `msg.payload` requirement and instead just use `msg` ?

Lets say I receive data:

```auto
{
"temperature":18,
"humidity":60,
"payload":"test"
}

```

Now I cannot access both the temperature and humidity at the same time using the switch node (perhaps with JSONata), I would like to have an option to just select the whole `msg`. In the beta the "has key" option" has been introduced that would work wonders in this situation.

Same goes for the inject node, I cannot simulate objects that don't have `msg.payload` as property.

Any thoughts ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 September 2019 11:10 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/2 "2019-09-10T11:10:49Z")

</div>

This would have to be considered on a node-by-node basis.

I can certainly see the benefit of the Switch node being able to operate on the whole message.

Separately, a more general Inject node has long been on the todo list.

Are there other examples you can think of where this would be useful?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 September 2019 11:27 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/3 "2019-09-10T11:27:55Z")

</div>

> [@knolleary](#):
>
> Separately, a more general Inject node has long been on the todo list.

Hi Nick, do you intend on permitting the inject to send to something other than `payload`? Ideally directly to `msg` like @bakman2 says. That would be a great help in testing (and many other scenarios too).

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 September 2019 11:41 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/4 "2019-09-10T11:41:43Z")

</div>

> This would have to be considered on a node-by-node basis.

It is also the nodes that produce/accept 'non-payload' based objects, perhaps some documentation guidance could help to avoid this (ie. use msg.payload for input/output).

I obviously could change them using a change node first, but it would be nice to skip a step.

This would apply to the switch, range, change, template and rbe nodes only I believe (dashboard?).

For the inject node - example; I have a milight, node accepts `msg.rgb` (among others). With the inject node I could create a interval schedule, but because I cannot send `msg.rgb` it cannot not be used for this purpose (this is an example, there are other options for the node to make it work).

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 September 2019 12:25 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/5 "2019-09-10T12:25:16Z")

</div>

> [@Steve-Mcl](#):
>
> directly to `msg`

Don't forget we also add other metadata to the msg object (like \_msgid) so it would not be guaranteed to be exactly as you set it.

One of the basic tenets of Node-RED is that the payload is where the "interesting" data is expected to be from node to node, with other properties being used to help "guide" that data - Other nodes should really expect this and be in line - but hey - authors are human and are free to do what they want.

But yes - a better inject has long been an idea - but needs to be simple... yet flexible.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [11 September 2019 04:37 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/6 "2019-09-11T04:37:39Z")

</div>

> [@dceejay](#):
>
> One of the basic tenets of Node-RED is that the payload is where the "interesting" data is expected to be from node to node

I have observed a number of contrib nodes that, in wishing to preserve the incoming data (via payload), add any required input (for their node) via msg.xyz. I thought this was accepted behaviour?

I have done this for the node I am working on. Should I rethink this?

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [11 September 2019 05:05 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/7 "2019-09-11T05:05:28Z")

</div>

I try to think of it this way:

- Use the `payload` for the data that is operated on in your node.
- Use other message properties to "parameterize" the operation.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [11 September 2019 06:34 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/8 "2019-09-11T06:34:44Z")

</div>

In general you should operate on the incoming payload and send your answer back out as the payload - ready for the next node. As noted if necessary you can add extra properties to help "describe" it if necessary. Also you should try to leave and other properties alone (unless of course you need to operate on them - i.e. only touch what your node needs to touch)

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [11 September 2019 07:39 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/9 "2019-09-11T07:39:16Z")

</div>

Perhaps this is considered trivial but in terms of best practice for node input (performing operations/changing parameters on the node), you are suggesting that only the first of the following is acceptable?

- msg.payload.xyz
- msg.node\_name.xyz
- msg.xyz

I understand that output should always be in the payload, and leave anything not needed to pass through unhindered.

Thanks

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 September 2019 07:50 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/10 "2019-09-11T07:50:44Z")

</div>

> I understand that output should always be in the payload

If the _output_ 'should' always be in the payload, then the _input_ 'should' also be in the payload (how else did it get there) 😉

In practice what I find is that certain properties cannot be accessed in 1 go with a switch/change node. If for example the switch node would have the flexibility to use `msg` as input then there is no issue. Currently there is no blocking issue either, as one can change objects/properties, albeit with an extra step/node.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [11 September 2019 08:06 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/11 "2019-09-11T08:06:21Z")

</div>

After all, the message is just an object that is passed between nodes. So, theoretically, you can do with it what you want. Just be aware, _not_ to delete or overwrite properties, that are required by nodes downstream.

So it all comes down to conventions and common practices, `msg.payload` being one of them.  
That makes it easier for the platform to be extended by custom nodes, and easier for the user to understand.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [11 September 2019 08:09 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/12 "2019-09-11T08:09:16Z")

</div>

The first is where most nodes will expect to find things so it makes sense to put it there..

```
msg.payload = "simple value";
msg.payload = {multiple:"property1", andAnother:"property2"}

```

and

```
msg.mymetadata = {"some extra property": "that helps my payload"}

```

but as I said - it's more guidance than a rule - so the following node can guess where to look to start with.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [11 September 2019 08:10 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/13 "2019-09-11T08:10:17Z")

</div>

> [@bakman2](#):
>
> If the _output_ 'should' always be in the payload, then the _input_ 'should' also be in the payload (how else did it get there) 😉

Should it? That's what I am asking. If you have an "intermediate" node that performs a certain task on the incoming payload, but requires "user intervention", and those user properties should not be passed on to the output ...

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [11 September 2019 08:12 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/14 "2019-09-11T08:12:03Z")

</div>

> [@dceejay](#):
>
> and

Ok, got it, thanks.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 September 2019 08:20 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/15 "2019-09-11T08:20:17Z")

</div>

> Should it? That's what I am asking. If you have an "intermediate" node that performs a certain task on the incoming payload, but requires "user intervention", and those user properties should not be passed on to the output ...

If this intermediate node cannot access `msg.x` but only `msg.payload.x` you need an another node to make it work. And you word it correctly: incoming `payload` 😉

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [11 September 2019 08:24 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/16 "2019-09-11T08:24:44Z")

</div>

Th ideal world would be that you just string together a flow on nodes from input to output without having to configure anything, and then hit deploy and it works... - so they should all expect to operate on some property and pass it on to the next. We chose msg.payload to be that. Of course in the real world you may need extra hints on how to handle that data so you know what to do with it - hence metadata.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [11 September 2019 08:45 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/17 "2019-09-11T08:45:26Z")

</div>

> [@dceejay](#):
>
> hence metadata.

Yes, if I had used that term to begin with ... But it wasn't around much (if at all) last century 😝

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 September 2019 08:48 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/18 "2019-09-11T08:48:45Z")

</div>

> [@Bobo](#):
>
> But it wasn't around much (if at all) last century

It was called "extra bits" in olden times 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [11 September 2019 08:55 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/19 "2019-09-11T08:55:21Z")

</div>

> [@Bobo](#):
>
> But it wasn't around much (if at all) last century

Neither was Node-RED !

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [11 September 2019 08:56 UTC](https://discourse.nodered.org/t/msg-payload-assumption/15357/20 "2019-09-11T08:56:41Z")

</div>

Must've been on the AS/400 surely. Everything else was.

[Next page](https://discourse.nodered.org/t/msg-payload-assumption/15357.md?page=2)
