Merge keys from 'schema' array into many products

Hi all!

I’m trying to process some product data but I’'m running into some issues.

The system I’'m extracting the data from supplies a schema which contains the fields in an array before supplying the actual products in arrays:

{
	"data": {
		"schema": [
			"id",
			"name",
			"description"
		],
		"products": [
			[
				"Value_1",
				"Value_2",
				"Value_3"
			],
			[
				"Value_X",
				"Value_Y",
				"Value_Z"
			],
			[
				"Value_A",
				"Value_B",
				"Value_C"
			]
		]
	}
}

As you might already guess the position of the fields matches the fields in the schema array.

Here’'s what I’'m aiming for:

{
	"data": {
		"schema": [
			"id",
			"name",
			"description"
		],
		"products": [
			{
				"id": "Value1",
				"name": "Value2",
				"description": "Value3"
			},
			{
				"id": "ValueX",
				"name": "ValueY",
				"description": "ValueZ"
			},
			{
				"id": "ValueA",
				"name": "ValueB",
				"description": "ValueC"
			}
		]
	}
}

I have been trying some different data transformers including a combination of different ones but I’m not able to get the result I desire. Help would be very much appreciated! :smiley:

Hi @Ties

You can use a Code Transformer as shown below.

Please find the code below.

const transformData = (data) => {
    const { schema, products } = data;
    const transformedProducts = products.map((productArray) => {
        let productObject = {};
        schema.forEach((key, index) => {
            productObject[key] = productArray[index];
        });
        return productObject;
    });

    return {
        schema: schema,
        products: transformedProducts
    };
};

return transformData(data);

I used the AI feature by clicking the “Generate” button to generate the code by adding a prompt and the sample data you provided.


Alternatively, you can also achieve it by using the native transformers. Please find the steps below. Please find the overview of the entity transformer below.

First, use Chain multiple entity transformers as we need to use “Data, transform data using mappers and conditions” and “Node, transform nodes” within a single entity transformer.

Add “Data, transform data using mappers and conditions” and put Move using pattern as the first transformer to make each value be in a property called value.

Then, use Recursively copy values to children to copy the keys to each value object.

Next, move the corresponding key based on the index of the value and put it in a property called key using “Move using a pattern”.

Finally, loop into the data.products array using “Node, transform nodes” and use “Move data between accessors” to transform the key and value properties of each value object into key: value.

Please find the exported configuration file of the above solution below:

forum-2769-merge-keys-from-schema-array-into-many-products.ndjson (1.7 KB)

Feel free to let me know if you have any questions.