# Setting multiple objects in payload

**URL:** <https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827>\
**Category:** General\
**Created:** [19 December 2023 04:20 UTC](https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827 "2023-12-19T04:20:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [19 December 2023 04:20 UTC](https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827/1 "2023-12-19T04:20:27Z")

</div>

Hello

I need to set multiple objects in a payload before sending.

I need it to look like:

payload  
pool\_api\_code: ABC  
temperature\_scaler: 1

I have tried:

msg.payload = {"pool\_api\_code": "ABC" + "temperature\_scaler": 1 }

but that does not work. Can anyone help me with the correct syntax?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [19 December 2023 05:20 UTC](https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827/2 "2023-12-19T05:20:25Z")

</div>

> I need to set multiple objects in a payload before sending.

Do you mean that you need to set multiple properties ?

There are multiple ways - the concept is:  
`{ property: value, property: value, ... }`

some examples

```auto
msg.payload = { pool_api_code:"ABC", temperature_scaler: 1 }
return msg

```

or

```auto
const payload = {}
payload.pool_api_code = "ABC"
payload.temperature_scaler = 1

msg.payload = payload
return msg

```

or (this will create the property names automatically)

```auto
const pool_api_code = "ABC"
const temperature_scaler = 1
msg.payload = {pool_api_code, temperature_scaler}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 December 2023 10:48 UTC](https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827/3 "2023-12-19T10:48:31Z")

</div>

> [@bakman2](#):
>
> ```auto
> const pool_api_code = "ABC"
> const temperature_scaler = 1
> msg.payload = {pool_api_code, temperature_scaler}
> 
> ```

I didn't know you could do that. Thanks for the education. Did that appear in a recent version of nodejs or have I just been asleep at the wheel?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [19 December 2023 11:10 UTC](https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827/4 "2023-12-19T11:10:16Z")

</div>

> [@Colin](#):
>
> I didn't know you could do that. Thanks for the education. Did that appear in a recent version of nodejs or have I just been asleep at the wheel?

I do this often, I think it's been around for some time, but can't find any reference to when it was a thing

**EDIT**  
Found - _The computed property names feature was introduced in ECMAScript 2015(ES6)_

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [19 December 2023 11:50 UTC](https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827/5 "2023-12-19T11:50:31Z")

</div>

> [@Colin](#):
>
> I didn't know you could do that. Thanks for the education. Did that appear in a recent version of nodejs or have I just been asleep at the wheel?

`Object.assign/create` may be a better choice if it is uncertain if the object changes afterwards.  
But this can also be very useful if you want to combine multiple objects into a single object, by using the spread operator:

```auto
const a = {a:true}
const b = {b:[],c:false}
const obj = {...a, ...b} // {"a":true,"b":[],"c":false}

```

Same can be done with arrays as well (also to get unique values with `[...new Set(array)]`, but we are getting distracted ;))

---

<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:** [17 February 2024 11:50 UTC](https://discourse.nodered.org/t/setting-multiple-objects-in-payload/83827/6 "2024-02-17T11:50:40Z")

</div>

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