Merge keys from 'schema' array into many products

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.