Change node and move array position

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

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:

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.

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

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.

1 Like

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