# Proper way of including variable in msg.payload

**URL:** https://discourse.nodered.org/t/proper-way-of-including-variable-in-msg-payload/30829
**Category:** General
**Created:** [28 July 2020 14:45 UTC](https://discourse.nodered.org/t/proper-way-of-including-variable-in-msg-payload/30829 "2020-07-28T14:45:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![eibyer\_nr](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@eibyer\_nr](https://discourse.nodered.org/u/eibyer_nr)
#### Post date: [28 July 2020 14:45 UTC](https://discourse.nodered.org/t/proper-way-of-including-variable-in-msg-payload/30829/1 "2020-07-28T14:45:51Z")

</div>

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

```auto
{
   "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"
      }
   }
}

```

---

<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: [28 July 2020 15:25 UTC](https://discourse.nodered.org/t/proper-way-of-including-variable-in-msg-payload/30829/2 "2020-07-28T15:25:11Z")

</div>

> [@eibyer\_nr](#):
>
> Do I have this formatted correctly?

I dont thing so.

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

> [@eibyer\_nr](#):
>
> 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

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

```auto
"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...

```auto
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) ...

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

---

<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: [11 August 2020 15:25 UTC](https://discourse.nodered.org/t/proper-way-of-including-variable-in-msg-payload/30829/3 "2020-08-11T15:25:13Z")

</div>

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