# Change node and move array position

**URL:** <https://discourse.nodered.org/t/change-node-and-move-array-position/21389>\
**Category:** General\
**Created:** [7 February 2020 09:59 UTC](https://discourse.nodered.org/t/change-node-and-move-array-position/21389 "2020-02-07T09:59:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [7 February 2020 09:59 UTC](https://discourse.nodered.org/t/change-node-and-move-array-position/21389/1 "2020-02-07T09:59:57Z")

</div>

Hi, I'm picking up data from database having a structure like "msg.payload[0].data[0]" as you can see with Debug\_1. I need to have a structure like "msg.payload.data". I've tryed several solution but the best I can reach is what you see with Debug\_2, where the first element of the array is empty, loosing in this way the previous "msg.payload[0].data[0][0]".  
Whot I'm doing wrong? Thanks a lot

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/350d356ced859845f02b39ed7e57ea84994816b4.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 February 2020 10:26 UTC](https://discourse.nodered.org/t/change-node-and-move-array-position/21389/2 "2020-02-07T10:26:17Z")

</div>

I think this is getting a little confused because you want the node to implicitly change `payload` from an Array to an Object. With the rules you are using, it doesn't know that, so it is reusing the existing property - which is an array.

To do the change you want, I'd do it like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ed77af8eeb64a8436003c9af0412414310044aa.png)

First move the data you want to a temporary property  
Then reset payload to be an empty Object  
Then move the temporary property back to where you want it.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [7 February 2020 12:35 UTC](https://discourse.nodered.org/t/change-node-and-move-array-position/21389/3 "2020-02-07T12:35:09Z")

</div>

Now I undestand it. really thanks a lot for your support 😉

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [10 February 2020 20:51 UTC](https://discourse.nodered.org/t/change-node-and-move-array-position/21389/4 "2020-02-10T20:51:09Z")

</div>

FYI, another way to "restructure" the payload (from an array of arrays to an object with a data field) is to use a `change` node with the Jsonata expression `{"data": payload[0].data[0]}`

Although these expressions can be a bit complicated to understand, I like to use them for these situations since they do not throw exceptions if there is a missing level of the expression path in the incoming payload. For example, if for some reason the payload were an object (instead of an array), referencing `msg.payload[0].data[0]` in a JS function would throw an exception -- but the expression above would return an empty object.

---

<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:** [24 February 2020 20:51 UTC](https://discourse.nodered.org/t/change-node-and-move-array-position/21389/5 "2020-02-24T20:51:17Z")

</div>

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