# How to aggregate multiple sub msg objects as one

**URL:** <https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 December 2022 00:42 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955 "2022-12-10T00:42:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xHUNx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhunx/32/72303_2.png) [@xHUNx](https://discourse.nodered.org/u/xHUNx)\
**Post date:** [10 December 2022 00:42 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/1 "2022-12-10T00:42:23Z")

</div>

Trying to write a function without much success:  
How to take a `msg` objects and sub objects and pass them as top `msg`.  
Example:  
`msg` has 3 objects (`payload,req,res`) and their sub objects.  
2 object that generated as part of the http in node [`res`,`req`]  
single `msg.payload` contains the following `{ "color":"red", "size":42 }`.

I wish to take the objects from the payload and move them up to the top level where the payload exist beside the `res,req`. and pass that newly crafted `msg` to the next node.

Hope that make sense.  
 ![Screenshot 2022-12-10 004116](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e01d38a1873b7495276815e4507d0175ad663c9.jpeg)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 December 2022 01:43 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/2 "2022-12-10T01:43:31Z")

</div>

Hi @xHUNx, welcome to the forum.

You just need a Change node like this

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c9e0cde9de4b372939858095b99c6ca3422b4e83.jpeg)

This will give you msg.color and msg.size. msg.payload will be empty.

---

<div class="post-metadata">

**Author:** ![xHUNx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhunx/32/72303_2.png) [@xHUNx](https://discourse.nodered.org/u/xHUNx)\
**Post date:** [10 December 2022 09:57 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/3 "2022-12-10T09:57:21Z")

</div>

I don't think it's going to work as the payload content is dynamic (constantly changing based on the API body that was submitted) and it's not constant color or size, it could be "move":"up" or "temperature":29

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 December 2022 11:04 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/4 "2022-12-10T11:04:34Z")

</div>

You can iterate through the elements of msg.payload in a function node like this

```auto
for (const key in msg.payload) {
   node.warn("Key " + key + " Value " + msg.payload[key])
}

```

The square brackets notation is needed becaue "msg.payload.key" would be interpreted as the literal name of an element of msg.payload.

---

<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:** [10 December 2022 11:25 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/5 "2022-12-10T11:25:51Z")

</div>

A neat way copy all the properties of the payload up to the message is

```auto
Object.keys(msg.payload).forEach(key => {
    msg[key] = msg.payload[key]})
return msg;

```

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [10 December 2022 21:10 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/6 "2022-12-10T21:10:07Z")

</div>

> [@Colin](#):
>
> `msg[key] = msg.payload[key]`

Nice. You might add

```auto
delete msg.payload[key]

```

---

<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:** [10 December 2022 21:45 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/7 "2022-12-10T21:45:37Z")

</div>

If necessary, yes. I did say that my code copied the values. If a move is necessary then they can be deleted from the payload as you suggest. Very likely it is not necessary though.

---

<div class="post-metadata">

**Author:** ![xHUNx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xhunx/32/72303_2.png) [@xHUNx](https://discourse.nodered.org/u/xHUNx)\
**Post date:** [11 December 2022 10:23 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/8 "2022-12-11T10:23:31Z")

</div>

indeed thanks all for help, I also added to delete the whole payload item as it's not needed there to be empty

```auto
Object.keys(msg.payload).forEach(key => {
    msg[key] = msg.payload[key]
    delete msg.payload[key]
})
delete msg.payload
return msg;

```

---

<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:** [11 December 2022 17:44 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/9 "2022-12-11T17:44:51Z")

</div>

If you have `delete msg.payload` then you don't need the `delete msg.payload[key]`. Are you sure you need to do that though? What is the benefit? If you do not need to do it then you are just adding lines of code and using up processor time for no purpose.

---

<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:** [25 December 2022 17:45 UTC](https://discourse.nodered.org/t/how-to-aggregate-multiple-sub-msg-objects-as-one/71955/10 "2022-12-25T17:45:26Z")

</div>

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