Creating MSG.objects on the fly

Hi

Following on from something like this - How to create msg keys? - #11 by waquete

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

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)

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.

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 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

The syntax is 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

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"

Of course it does - as this is what you asked to do. :wink:
If label is a variable holding a string or a number, you can create an objects property by 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

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 = [];
           
         
         }

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