# Creating MSG.objects on the fly

**URL:** https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692
**Category:** General
**Tags:** function-node
**Created:** [10 July 2023 11:04 UTC](https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692 "2023-07-10T11:04:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)
#### Post date: [10 July 2023 11:04 UTC](https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692/1 "2023-07-10T11:04:42Z")

</div>

Hi

Following on from something like this - [How to create msg keys? - #11 by waquete](https://discourse.nodered.org/t/how-to-create-msg-keys/51777/11)

I'd like to create adhoc object key names depending on the contents of the incoming payload.

For example, I never know what the payload will contain, but I can strip it into the component parts.  
IE

```auto
508 transport info:
status: play
speed: 100

```

A simple `.split("\n")` gives me an array containing each line.

I can split each link to give me the pairs of data (IE, Label and Value)

```auto
replies: array[3]
0: "508 transport info:
"
1: "status: play
"
2: "speed: 100
"

```

What I really want to do is create a new MSG.`object` using the Label and put the Value in place.

```plaintext
var part = [];
var label = [];
var value = [];
msg.decode =[];

           while (n < replies.length){
                             part =replies[n].split(":");
                             node.status({fill:"red",shape:"ring",text:"n = "+n});

                     if (part[0].length > 1 && part[1].length > 1){  
                     

                             label =part[0].replace(" ","").replace("\n","").replace("\r","");
                            value =part[1].replace(" ","").replace("\n","").replace("\r","");
                            
                      msg.decode[n] = {[label]:value};

                      
                     }
                     
                     
                 n = n + 1;
                 part = [];
                 label = [];
                 value = [];
           
         
         }

```

which gives me something like  
decode: array[3]  
0: undefined  
1: object  
status: "play"  
2: object  
speed: "100"

This gets me close, but all it's done is make an array of objects again, which doesn't work for me further down the flow when I'm trying trigger events based on the existent of a MSG object (or not)

What I really would like to see is

msg.status = "play"  
msg.speed = "100"  
msg.timecode = "867"  
msg.clipid = "1"

and so on  
(I can deal with converting Strings to Numbers later if needed, but most of the time they are simply being pushed back out as String commands and the next device doesn't care)

in my noob mind I thought that I can create the msg object a little like this  
`msg.'label' = value`

I'm sure I'm not the first to ask this question, but I've searched for hours trying to find a similar question / answer.

* * *

For those that are interested in the "why", I'm working with [BlackMagicDesign HyperDecks](https://www.blackmagicdesign.com/uk/products/hyperdeckstudio) and trying to sync multiple decks to clips and timecode positions, to avoid the slightest drift

A basic test worked really well, until an unexpected payload arrives with "extra" data, then it all goes wrong.  
So I need a solution that can tolerate these extra bits of data

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [10 July 2023 11:11 UTC](https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692/2 "2023-07-10T11:11:04Z")

</div>

> [@MDAR](#):
>
> in my noob mind I thought that I can create the msg object a little like this  
> `msg.'label' = value`

The syntax is `msg['label'] = value`.

---

<div class="post-metadata">

### Author: ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)
#### Post date: [10 July 2023 11:15 UTC](https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692/3 "2023-07-10T11:15:23Z")

</div>

> [@ralphwetzel](#):
>
> msg['label'] = value

I got excited then...

But sadly that doesn't work.

All I get is the last value from the while loop saved as **msg.label**

like this

```auto
payload: string
508 transport info:
status: stopped
speed: 0

topic: ""
deck: 1
replies: array[5]
0: "508 transport info:
"
1: "status: stopped
"
2: "speed: 0
"
3: "
"
4: ""
decode: object
    1: object
        status: "stopped"
    2: object
        speed: "0"
label: "0"

```

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [10 July 2023 11:35 UTC](https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692/4 "2023-07-10T11:35:00Z")

</div>

> [@MDAR](#):
>
> All I get is the last value saved as msg.label

Of course it does - as this is what you asked to do. 😉  
If `label` is a variable holding a `string` or a `number`, you can create an objects property by `msg[label] = value`.

---

<div class="post-metadata">

### Author: ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)
#### Post date: [10 July 2023 11:39 UTC](https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692/5 "2023-07-10T11:39:11Z")

</div>

> [@ralphwetzel](#):
>
> `msg[label] = value`.

I have no idea why that didn't work the first time I tried it, about an hour ago...

But it **IS** working now.

Thank you for your amazing help

* * *

For those stumbling across this in the future, here is the working loop

Incoming array stored in `replies`

I'm happy to share the full flow if anyone is interested

```auto
var n =0;
var part = [];
var label = [];
var value = [];

           while (n < replies.length){
                             part =replies[n].split(":");
                             node.status({fill:"red",shape:"ring",text:"n = "+n});

                     if (part[0].length > 1 && part[1].length > 1){  
                     

                             label =part[0].replace(" ","").replace("\n","").replace("\r","");
                            value =part[1].replace(" ","").replace("\n","").replace("\r","");
                            
            msg[label] = value
                      
                     }
                     
                     
                 n = n + 1;
                 part = [];
                 label = [];
                 value = [];
           
         
         }

```

---

<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: [24 July 2023 11:39 UTC](https://discourse.nodered.org/t/creating-msg-objects-on-the-fly/79692/6 "2023-07-24T11:39:17Z")

</div>

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