How to swap columns with rows?

Hello
How to swap columns with rows?

Is there such a possibility in the function node?

Hi Roket! Can you please elaborate? We are not exactly sure what you are looking for.

I have a json from a webhook that always has 4 columns and a different number of rows. I would like to convert this structure to 4 rows and a different number of columns. And always equate the column keys to the “uuid” values of the converted strings.

From

To

Did I explain my task clearly? Are there any options for solving my problem?

Can anyone help me?
I have the ability to influence the incoming JSON, maybe there are other workarounds to solve my problem?

Hi @Rocket

What you are trying to achieve is a pivot table. I don’t think there is nothing built in to n8n to do this, but you can achieve this using the function node.

The function node receives all the input as items and you can freely transform it, outputting another array of elements.

Important note: all input elements contain a json property with their data, and the output needs to be an array of objects containing a json property as well. Quick example:

let output = [];
items.map((element, index) => {
	// element.json contains all information for line `index`
	let i = 0;
	for(key in element) {
		if (element.hasOwnProperty(key)) {
			if (output[i] === undefined) {
				output[i] = {"json": {}};
			}
			output[i].json[index] = element[key];
			i++;
		}
	}
});
return output;

This is a simple pivot structure. You can improve column names and choose what to transpose, but in general, this is probably what you are looking for I believe.

Thank you for your attention to my problem, but this solution did not work for me. I am not a programmer and have no JS conversion skills.
Applying your code I got the following (.

And I need such a data structure.

Perhaps, it is not quite clear to you because of the screenshots in Russian? I can make an example problem in English, if it helps to understand better.

Hey @Roket

The example I provided is just a sample of what you can do, but should not be a definitive solution to your problem.

Unfortunately I cannot create a specific solution for this right now, but you mentioned you can change the incoming JSON, can you make it already to the format you expect? That might solve your issue.

1 Like

Thanks
I’ll try another way

@Roket while I cannot offer a solution at the moment for your case - I do want to say that we are planning some transform nodes to limit the need to use Function node for typical transforms. Adding your case to the list!

1 Like

It would be great!
Thank you for your attention to the details of my problem.

1 Like

Hi again!
Solved the problem on the side of generating incoming JSON.
Thank you all for your attention to my task.

2 Likes