Create comma separated string from array

Hi all,

I have a HTTP module that returns an array of collections with values including a name, description and number.
Rough response is:
items: [
{
name: name 1,
description: description1,
number: number 1
},
{
name: name 2,
description: description2,
number: number 2
},
{
name: name 3,
description: description3,
number: number 3
},
]

I want to extract those values for each collection and create a comma separated string in the format of name1 | description1 | number1, name2 | description2 | number 2, etc.

I’m trying to achieve this through a Text Aggregator, however, the closest I can get to the desired string is the format of name1,name2,name3 | description1,description2,description3 | number1, number2, number3.

Here is my Text Aggregator setup:

How can I change this to get the desired structure?

Thanks

If you can share the blueprint, I could take a look and offer more help.

First thought, I would try using:

1. An Iterator module to process each item in the array; then
2. A Text Parser to concatenate each iteration (for each item processed by the Iterator) of name , description , and number into the desired format name | description | number
A. In the module, create a string in the format ${name} | ${description} | ${number} by referencing the fields from each iterated item.
3. Next, an Aggregator module to collect the formatted strings back into an array; then
4. Another Text Parser to join the array of formatted strings into a single comma-separated string using a join command

Hope this helps.

2 Likes

Welcome to the Make community!

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

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

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": 37,
                    "module": "builtin:BasicFeeder",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "array": "{{36.items}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 589,
                            "y": -1653
                        },
                        "restore": {
                            "expect": {
                                "array": {
                                    "mode": "edit"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "array",
                                "type": "array",
                                "label": "Array",
                                "mode": "edit",
                                "spec": []
                            }
                        ]
                    }
                },
                {
                    "id": 38,
                    "module": "util:TextAggregator",
                    "version": 1,
                    "parameters": {
                        "rowSeparator": "other",
                        "otherRowSeparator": ", ",
                        "feeder": 37
                    },
                    "mapper": {
                        "value": "{{37.name}} | {{37.description}} | {{37.number}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 834,
                            "y": -1654,
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {
                            "parameters": {
                                "rowSeparator": {
                                    "label": "Other"
                                }
                            },
                            "extra": {
                                "feeder": {
                                    "label": "Iterator [37]"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "rowSeparator",
                                "type": "select",
                                "label": "Row separator",
                                "validate": {
                                    "enum": [
                                        "\n",
                                        "\t",
                                        "other"
                                    ]
                                }
                            },
                            {
                                "name": "otherRowSeparator",
                                "type": "text",
                                "label": "Separator"
                            }
                        ],
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "advanced": true
                    },
                    "flags": {
                        "stopIfEmpty": true
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
3 Likes

Awesome, thanks @rthidden and @samliew - Sam’s option worked a treat. So simple, yet to effective. Not sure how I didn’t land on that one - spent way too long on that one.

3 Likes