# Create an object from msg.payload

**URL:** <https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856>\
**Category:** General\
**Created:** [12 February 2021 07:59 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856 "2021-02-12T07:59:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [12 February 2021 07:59 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/1 "2021-02-12T07:59:16Z")

</div>

Hi,  
can someone please tell me how to create an object from any number of messages from my flow?

```auto
msg1 = {};
msg1.payload = {"Spreizung KT": msg.payload};
msg2 = {};
msg2.payload = {"THub": Number(msg.payload)};
msg3 = {};
msg3.payload = {"Leistung nach ZH": msg.payload};
return msg1,msg2,msg3;

```

this function doesn´t work, it makes 3x objects.

the output of the function should be like the buffer parser output:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/8103f6fff6a9c260acc493d59136b3f862df8a86.png)

Thanks.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 February 2021 08:06 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/2 "2021-02-12T08:06:58Z")

</div>

```auto
let msg1 = {"payload":{}};
msg1.payload.Spreizung KT= msg.payload;
msg1.payload.THub=Number(msg.payload);
msg1.payload["Leistung nach ZH"]=msg.payload;
return msg1;

```

---

<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:** [12 February 2021 08:08 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/3 "2021-02-12T08:08:31Z")

</div>

> [@Becker](#):
>
> this function doesn´t work, it makes 3x objects

Then dont make 3 objects - update the payload of 1 object instead.

```auto
msg.payloadOrig = msg.payload;
msg.payload = {
  "Spreizung_KT": msg.payload,
  "THub": Number(msg.payload),
  "Leistung_nach_ZH": msg.payload
}
return msg;

```

PS...  
for debugging and other reasons I wont go into right now, it is usually best to re-use the original `msg` object instead of creating new objects.

---

<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:** [12 February 2021 08:10 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/4 "2021-02-12T08:10:28Z")

</div>

> [@E1cid](#):
>
> ```auto
> msg1.payload.Spreizung KT= msg.payload;
> msg1.payload.THub=Number(msg.payload);
> msg1.payload.Leistung nach ZH=msg.payload;
> 
> ```

This wont work - you cant have spaces in a property name unless you use `[]` square bracket notation.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 February 2021 08:12 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/5 "2021-02-12T08:12:14Z")

</div>

Cheers missed the spaces, edited code.

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [12 February 2021 15:13 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/6 "2021-02-12T15:13:11Z")

</div>

I´m a fool, just finished taking the join node 🤪 🥴  
sorry  
javascript is tough

---

<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:** [12 February 2021 15:27 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/7 "2021-02-12T15:27:07Z")

</div>

Most things are tough initially. Don't worry about it.

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [12 February 2021 16:16 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/8 "2021-02-12T16:16:24Z")

</div>

i always like to learn new things. 🤓  
Javascript can do so much and I probably don't even know 1% of it.

---

<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:** [13 April 2021 16:16 UTC](https://discourse.nodered.org/t/create-an-object-from-msg-payload/40856/9 "2021-04-13T16:16:26Z")

</div>

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