# How to create msg keys?

**URL:** <https://discourse.nodered.org/t/how-to-create-msg-keys/51777>\
**Category:** General\
**Created:** [2 October 2021 12:48 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777 "2021-10-02T12:48:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![waquete](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@waquete](https://discourse.nodered.org/u/waquete)\
**Post date:** [2 October 2021 12:48 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/1 "2021-10-02T12:48:51Z")

</div>

Hi !  
I have some trouble creating "sub-keys" in msg.payload.  
This is for telegram bot, I need:  
msg.payload.content  
msg.payload.chatId  
msg.payload.type

I want to build the message in a function node.

Sometime using this is good enough:  
msg.payload.type = "message";

But sometime is just says "msg.payload.type is empty" in debug window.

I found a workaround by creating the message this way:  
msg = { payload: { chatId : 11111111111 , type : "message" , content : "Hello World" } };

(1111111111 is a dummy value)

But it's a bit "heavy" (in my opinion) sometime and I would prefer set the keys one by one, when I need.

The point is that I don't understand exactly what's the issue, so if I missed something simple, I would be happy to learn 🙂  
If there's a better way to create sub-key of msg or msg.payload, I would be happy to know too 🙂

---

<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:** [2 October 2021 13:04 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/2 "2021-10-02T13:04:30Z")

</div>

> [@waquete](#):
>
> If there's a better way to create sub-key of msg or msg.payload, I would be happy to know too

Set msg.payload to a new object before adding properties...

```auto
msg.payload = {};
msg.payload.type = 'whatever';

```

---

<div class="post-metadata">

**Author:** ![waquete](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@waquete](https://discourse.nodered.org/u/waquete)\
**Post date:** [2 October 2021 13:37 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/3 "2021-10-02T13:37:53Z")

</div>

Wow, seems to work perfectly !

is it ok to add as many keys I need? I mean:

```auto
msg.payload = {};
msg.payload.type = 'whatever';
msg.payload.content ='anythingelse';
msg.payload.somestuff = 123;
...

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [2 October 2021 13:39 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/4 "2021-10-02T13:39:56Z")

</div>

Yes - you are spot on with that. It also makes it easy to understand visually what's going on.

You can also include in your object...

msg.payload.chatId =123456789;

---

<div class="post-metadata">

**Author:** ![waquete](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@waquete](https://discourse.nodered.org/u/waquete)\
**Post date:** [2 October 2021 13:52 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/5 "2021-10-02T13:52:16Z")

</div>

nice! I will update my flows this way.

It rise me a question, can I add a key to msg without losing other keys ?

In the same way `msg.payload = {}` initialise a new payload, I assume `msg = {};` would initialise a new msg (empty).

But say I want to keep `msg.payload`, `msg.topic`, whatever, as they are....but I wish to add a key `msg.limit` ?

---

<div class="post-metadata">

**Author:** ![waquete](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@waquete](https://discourse.nodered.org/u/waquete)\
**Post date:** [2 October 2021 13:56 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/6 "2021-10-02T13:56:28Z")

</div>

maybe simple by using ?  
`msg.limit = {};`

---

<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:** [2 October 2021 14:19 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/7 "2021-10-02T14:19:21Z")

</div>

> [@waquete](#):
>
> It rise me a question, can I add a key to msg without losing other keys

Yes if the payload is already an object.

You can test the payload is an object (and create an object if it is not)

```auto
if( typeof msg.payload !== "object" ) {
    msg.payload = {};
}
msg.payload.type = 'whatever';
msg.payload.content ='anythingelse';
msg.payload.somestuff = 123;

```

---

<div class="post-metadata">

**Author:** ![waquete](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@waquete](https://discourse.nodered.org/u/waquete)\
**Post date:** [2 October 2021 14:28 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/8 "2021-10-02T14:28:07Z")

</div>

That's clarify the reason why my initial attempt to simply add `msg.payload.type = ...` (without `msg.payload = {};` ) sometimes worked, sometimes not.  
I assume it depends if the payload was already an object or not.  
I think it's now very clear on how to manage msg.payload, thank you! 🙂

But I'm still not sure to understand how to add a key to the root `msg` without loosing other keys.  
For example, how to properly add a non existing `msg.limit` to `msg`, without effect on `msg.payload`

---

<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:** [2 October 2021 14:30 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/9 "2021-10-02T14:30:37Z")

</div>

`msg` is always an object. If yo want to add `limit` then...

```auto
msg.limit = {}; 
msg.limit.whatever = 123;

```

And just like the payload, you can test limit to see if it an object already.

This will not affect msg.payload. Just DONT set `msg` and all of its properties will remain intact.

Sky is the limit.

---

<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:** [2 October 2021 14:32 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/10 "2021-10-02T14:32:38Z")

</div>

> [@waquete](#):
>
> msg = { payload: { chatId : 11111111111 , type : "message" , content : "Hello World" } };

This ☝ is what causes msg and all its existing properties to be lost.

---

<div class="post-metadata">

**Author:** ![waquete](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@waquete](https://discourse.nodered.org/u/waquete)\
**Post date:** [2 October 2021 14:37 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/11 "2021-10-02T14:37:52Z")

</div>

ah ok, this rewrite the whole msg.  
I could use maybe:

```auto
msg.payload = {};
msg.payload.chatId : 11111111111;
msg.payload.type : "message";
msg.payload.content : "Hello World";

```

or directly:

```auto
msg.payload = {};
msg.payload = { chatId : 11111111111 , type : "message" , content : "Hello World" };

```

---

<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:** [2 October 2021 14:42 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/12 "2021-10-02T14:42:07Z")

</div>

> [@waquete](#):
>
> or directly:
> 
> ```auto
> msg.payload = {};
> msg.payload = { chatId : 11111111111 , type : "message" , content : "Hello World" };
> 
> ```

If setting directly, no need to create object...

```auto
msg.payload = { chatId : 11111111111 , type : "message" , content : "Hello World" };

```

☝ That's is all that's required.

---

<div class="post-metadata">

**Author:** ![waquete](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@waquete](https://discourse.nodered.org/u/waquete)\
**Post date:** [2 October 2021 15:04 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/13 "2021-10-02T15:04:10Z")

</div>

ah gosh, I see, in this case it both create and fill the object in a single line 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 December 2021 15:04 UTC](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/14 "2021-12-01T15:04:52Z")

</div>

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