Converting array with collections from 3 collections of 10 items to 10 collections of 3 items

I want to create a simple CSV but face a difficulty of “inverting the data”. I dont know if I describe it well but right now my scenario is creating an array of 3 collections, each collection contains 10 items (key:value). When I want to create a CSV it generates new row from collections so I will get 3 rows wich 10 columns.
What I want is to get result of 3 columns , each containing every item of the collections in the same order. Basically I want to make each array become one column

I have no idea how to invert this.

Welcome to the Make community!

Please provide the output bundles of the Array Aggregator module [118] by running the scenario, then click the white speech bubble on the top-right of each module and select “Download output bundles”.
Screenshot_2023-10-06_141025

A.

Save the bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.
Uploading it here will look like this:

bundle.txt (12.3 KB)

B.

If you are unable to upload files on this forum, alternatively you can paste the formatted output bundle in this manner:

  • Either add three backticks ``` before and after the code, like this:

    ```
    input/output bundle content goes here
    ```

  • Or use the format code button in the editor:
    Screenshot_2023-10-02_191027

Providing the output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

This will allow others to better assist you. Thanks!

2 Likes
[
    {
        "array": [
            {
                "Nowe": 4,
                "Stracone": 9,
                "prowizja": 435,
                "koszt reklamy": 388,
                "nazwa kampani": "campaign name1",
                "Czekam na kontakt": 2
            },
            {
                "Nowe": 1,
                "Stracone": 1,
                "prowizja": 0,
                "koszt reklamy": 4,
                "nazwa kampani": "campaign name 2",
                "Czekam na kontakt": 0
            },
            {
                "Nowe": 1,
                "Stracone": 2,
                "prowizja": 90,
                "koszt reklamy": 350,
                "nazwa kampani": "campaign name3",
                "Czekam na kontakt": 0
            }
        ],
        "__IMTAGGLENGTH__": 3
    }
]

Use an iterator to iterate over each array
then use a text aggregator to get the desired output



blueprint (6).json (5.3 KB)

2 Likes

It doesn’t give the result that I wanted, what you did is giving exactly the same results as I had before. You are getting 3 rows with 6 columns , I need to invert it and get each row as a column. So I will get column 1 with every value of 1st array, column 2 with every value of 2nd array and so on…

Welcome to the Make community!

Yes, that is possible. You’ll need a minimum of four modules:

1. Iterate the KEYS of the first item in the array

Screenshot_2024-01-26_150149

2. Map the array values in the original array, based on KEY (Iterator value)

3. Aggregate to Array

4. Convert it back to a collection

Output

Original: 3 items X 6 keys

Output: 6 keys X 3 items

Give it a go and let us know if you have any issues!

2 Likes

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV to paste in the canvas.

  3. Click on each imported module and save it. You may need to remap some variables.

Modules JSON Export

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 39,
                    "module": "json:ParseJSON",
                    "version": 1,
                    "parameters": {
                        "type": ""
                    },
                    "mapper": {
                        "json": "[\n    {\n        \"array\": [\n            {\n                \"Nowe\": 4,\n                \"Stracone\": 9,\n                \"prowizja\": 435,\n                \"koszt reklamy\": 388,\n                \"nazwa kampani\": \"campaign name1\",\n                \"Czekam na kontakt\": 2\n            },\n            {\n                \"Nowe\": 1,\n                \"Stracone\": 1,\n                \"prowizja\": 0,\n                \"koszt reklamy\": 4,\n                \"nazwa kampani\": \"campaign name 2\",\n                \"Czekam na kontakt\": 0\n            },\n            {\n                \"Nowe\": 1,\n                \"Stracone\": 2,\n                \"prowizja\": 90,\n                \"koszt reklamy\": 350,\n                \"nazwa kampani\": \"campaign name3\",\n                \"Czekam na kontakt\": 0\n            }\n        ],\n        \"__IMTAGGLENGTH__\": 3\n    }\n]"
                    },
                    "metadata": {
                        "designer": {
                            "x": -1124,
                            "y": -1087
                        },
                        "restore": {
                            "parameters": {
                                "type": {
                                    "label": "Choose a data structure"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "type",
                                "type": "udt",
                                "label": "Data structure"
                            }
                        ],
                        "expect": [
                            {
                                "name": "json",
                                "type": "text",
                                "label": "JSON string",
                                "required": true
                            }
                        ]
                    }
                },
                {
                    "id": 40,
                    "module": "builtin:BasicFeeder",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "array": "{{keys(first(39.array))}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -880,
                            "y": -1087
                        },
                        "restore": {
                            "expect": {
                                "array": {
                                    "mode": "edit"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "array",
                                "type": "array",
                                "label": "Array",
                                "mode": "edit",
                                "spec": []
                            }
                        ]
                    }
                },
                {
                    "id": 42,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "array",
                        "scope": "roundtrip",
                        "value": "{{map(39.array; toString(40.value[]))}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -635,
                            "y": -1086
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "array",
                                "label": "array",
                                "type": "any"
                            }
                        ]
                    }
                },
                {
                    "id": 43,
                    "module": "builtin:BasicAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 40
                    },
                    "mapper": {
                        "array": "{{42.array}}",
                        "value": "{{40.value}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -388,
                            "y": -1086
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "Iterator [40]"
                                },
                                "target": {
                                    "label": "Custom"
                                }
                            }
                        }
                    },
                    "flags": {
                        "stopIfEmpty": true
                    }
                },
                {
                    "id": 44,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "inverted",
                        "scope": "roundtrip",
                        "value": "{{toCollection(43.array; \"value\"; \"array\")}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -138,
                            "y": -1089
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "label": "Variable name",
                                "type": "text",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "label": "Variable lifetime",
                                "type": "select",
                                "required": true,
                                "default": "roundtrip",
                                "options": [
                                    {
                                        "label": "One cycle",
                                        "value": "roundtrip"
                                    },
                                    {
                                        "label": "One execution",
                                        "value": "execution"
                                    }
                                ]
                            },
                            {
                                "name": "value",
                                "label": "Variable value",
                                "type": "any"
                            }
                        ],
                        "interface": [
                            {
                                "name": "inverted",
                                "label": "inverted",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
3 Likes

WOW this is great, i am doing some complicated stuff in make but this is magic. The only problem is I still dont know how to put it into csv in the way I wanted xD

2 Likes


this is the 2nd set variable module

this is the last iterator (before csv)

this is CSV generator

I dont know how to describe it but I want to iterate each array, instead of having 1 bundle with each one. It would be the best to have array name as 1 value of array.

Zrzut ekranu 2024-01-26 o 13.32.39

Welcome to the Make community!

That looks like a new question, could you please create a separate topic for this?

While it’s tempting to continue an existing thread, a more effective approach would be to start a new topic just for your next question. It helps community experts find and respond to your query quicker, and keeps our space organised for everyone. If you start a new conversation you are also more likely to get help from other users. You can refer others back to a related topic by including that link in your question. Thank you for understanding and keeping our community neat and tidy.

The “New Topic” link can be found in the top-right of the header:

Screenshot_2023-12-19_091207

3 Likes

thanks, it turned out I that the data I need was halfway of the scenario that you proposed.
Anyway big kudos for your knowledge.

3 Likes