Proper way of including variable in msg.payload

Do I have this formatted correctly? I'm using http request node to hit an endpoint with the data payload.

msg.payload = ``{ "data": {{mac1Status}, {mac2Status}, {mac3Status}, {mac4Status}} }`;
return msg;

Note: There's an extra back tick before word data because the forum is treating the text as code if I don't add the extra one.

The data structure looks like this....

{
   "data":{
      "person1":{
         "status":"Disconnected",
         "mac":"3c:00:6d:14:3a:f1"
      },
      "person2":{
         "status":"Connected",
         "mac":"44:91:60:d5:00:f6"
      },
      "person3":{
         "status":"Connected",
         "mac":"58:cb:00:5c:58:45"
      },
      "person4":{
         "status":"Connected",
         "mac":"c4:98:80:db:00:5e"
      }
   }
}

I dont thing so.

It looks like you are trying to make JSON (string) when you really only need to do javascript.

a ` tells the forum to format as code. Use a \ to escape them e.g. \`

So, on you your problem...

to get a payload of ...

"data":{
      "person1":{
         "status":"Disconnected",
         "mac":"3c:00:6d:14:3a:f1"
      },
      "person2":{
         "status":"Connected",
         "mac":"44:91:60:d5:00:f6"
      },
      "person3":{
         "status":"Connected",
         "mac":"58:cb:00:5c:58:45"
      },
      "person4":{
         "status":"Connected",
         "mac":"c4:98:80:db:00:5e"
      }
   }

you need to do something like this...

msg.payload = {
   "data":{
      "person1":{
         "status":"Disconnected",
         "mac":"3c:00:6d:14:3a:f1"
      },
      "person2":{
         "status":"Connected",
         "mac":"44:91:60:d5:00:f6"
      },
      "person3":{
         "status":"Connected",
         "mac":"58:cb:00:5c:58:45"
      },
      "person4":{
         "status":"Connected",
         "mac":"c4:98:80:db:00:5e"
      }
   }
}

So onto making it dynamic (using your mac1Status, mac2Status variables) ...

msg.payload = {
   "data":{}
}
msg.payload.data.person1 = {
  status: mac1Status.status,
  mac: mac1Status.mac,
}
msg.payload.data.person2 = {
  status: mac2Status.status,
  mac: mac2Status.mac,
}

^ but I am assuming the variable mac1Status is an object with properties .status and .mac

if you provide more info - we can better help

1 Like

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