Aggregating an array into a string with key value pairs

I have an array and want to flatten it into a single string (see screenshot) with the goal of using it as a task description and slack notification message body.

The string should include the key-value pairs, so in this case it would be:
“output”:
Organization name: example
Writing services: Write email
Design services: Template email (reusable sections)
Comments: examples”

I figured a text aggregator would help, but the key values vary depending on the form submission and so I need to get the keys and the values somehow, and there may be any number of itemOptions.

Thanks!

1 Like

Set Variable might be the wrong module to use.

But I can’t assist you because you’ve only shown the output of the set variable module and not your full scenario.

For further assistance, please provide the full scenario:

1. Screenshots of module fields and filters

Please share screenshots of relevant module fields and filters in question? It would really help other community members to see what you’re looking at.

You can upload images here using the Upload icon in the text editor:
Screenshot_2023-10-07_111039

2. Scenario blueprint

Please export the scenario blueprint file to allow others to view the mappings and settings. At the bottom of the scenario editor, you can click on the three dots to find the Export Blueprint menu item.

Screenshot_2023-08-24_230826
(Note: Exporting your scenario will not include private information or keys to your connections)

Uploading it here will look like this:

blueprint.json (12.3 KB)

3. And most importantly, Output bundles

Please provide the output bundles of the modules 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.

Following these steps will allow others to assist you here. Thanks!

samliewrequest private consultation

Join the unofficial Make Discord server to chat with us!

1 Like

Thanks Sam!

blueprint (1).json (31.7 KB)

Item Options output

    {
        "itemOptions": [
            {
                "Organization name": "client A"
            },
            {
                "Writing services": "Write email"
            },
            {
                "Design services": "Template email (reusable sections)"
            },
            {
                "Comments": "test"
            }
        ]
    }
]```

Could you please also provide the output bundle of your iterator module?

1 Like

Hey @samliew, yes! Here it is:

    {
        "id": "id",
        "order_id": "order_id",
        "item_id": "item_id",
        "item_name": "Email campaign",
        "item_sku": null,
        "item_sku_id": null,
        "item_variation_sku": null,
        "item_sku_attachment": null,
        "item_options": [
            {
                "Organization name": "Client A"
            },
            {
                "Writing services": "Writing service A"
            },
            {
                "Design services": "Design service B"
            },
            {
                "Comments": "test comment"
            }
        ],
        "item_image": "image.png",
        "unit_price": "0.00",
        "quantity": "5",
        "weight": "0.00",
        "item_tax_meta": null,
        "discount_name": null,
        "discount_amount": null,
        "sale_amount": null,
        "item_license_key": null,
        "item_source_url": "test",
        "status": "1",
        "reviewed_at": null,
        "deleted_at": null,
        "created_at": "2024-05-11 13:37:46",
        "updated_at": null,
        "product_id": "test_product_id",
        "product_name": "Email campaign",
        "product_description": "",
        "product_price": "195.00",
        "product_email_info": "test info",
        "product_quantity": null,
        "require_delivery_or_collection": "0",
        "shipping_type": "global",
        "min_order_quantity": null,
        "max_order_quantity": null,
        "sku": "SEND-KOG",
        "use_license_key_count_as_stock": "0",
        "categories": [
            {
                "id": "cat_6633e1936571b618828100",
                "category_name": "Send",
                "category_slug": "send",
                "status": "1"
            }
        ],
        "__IMTINDEX__": 1,
        "__IMTLENGTH__": 2
    },
    {
        "id": "oi_663f74aa54a94606483869",
        "order_id": "ord_663f74aa4891c709015972",
        "item_id": "prod_6633ee3d6689f439733005",
        "item_name": "Email campaign",
        "item_sku": null,
        "item_sku_id": null,
        "item_variation_sku": null,
        "item_sku_attachment": null,
        "item_options": [
            {
                "Organization name": "Client B"
            },
            {
                "Writing services": "Writing service C"
            },
            {
                "Design services": "Writing service D"
            },
            {
                "Comments": "test comments"
            }
        ],
        "item_image": "image.png",
        "unit_price": "295.00",
        "quantity": "1",
        "weight": "0.00",
        "item_tax_meta": null,
        "discount_name": null,
        "discount_amount": null,
        "sale_amount": null,
        "item_license_key": null,
        "item_source_url": "test",
        "status": "1",
        "reviewed_at": null,
        "deleted_at": null,
        "created_at": "2024-05-11 13:37:46",
        "updated_at": null,
        "product_id": "test",
        "product_name": "Email campaign",
        "product_description": "",
        "product_price": "195.00",
        "product_email_info": "test",
        "product_quantity": null,
        "require_delivery_or_collection": "0",
        "shipping_type": "global",
        "min_order_quantity": null,
        "max_order_quantity": null,
        "sku": "SEND-KOG",
        "use_license_key_count_as_stock": "0",
        "categories": [
            {
                "id": "cat_6633e1936571b618828100",
                "category_name": "Send",
                "category_slug": "send",
                "status": "1"
            }
        ],
        "__IMTINDEX__": 2,
        "__IMTLENGTH__": 2
    }
]```

To grab the key name of each of the collections you have to reference it as follows:

{{keys(33.itemOptions.1)}} to get the first collection’s key name (ie Organization name)

{{keys(33.itemOptions.2)}} will get the second and so on

The length(itemOptions) will return how many items are in the array, so the trick is how to construct a dynamic set of expressions necessary to get all of the potential collections that could come up in itemOptions… I think you need a repeater here that runs on length(itemOptions) but how to stick the numeric value into the expression evades me.

2 Likes

Well… This was surprisingly difficult to do for a dynamic set of collections. You really can’t use an iterator to accomplish this because you can’t reference the array index value dynamically using keys() function. Or at least I don’t know how to form the expression keys(arrayname.indexvalue).

To get a dynamic set of keys from a dynamic set of collections in itemOptions you can instead transform your webhook output to JSON and then use a Text Parser to extract the keys and values directly from the JSON text, and then text aggregate… This is definitely a workaround but it works great.

This regular expression will extract the key and the value from your JSON string that’s generated in module 43

"(?<key>[^"]+)"\s*:\s*"(?<value>[^"]+)"

The text aggregator just collects the found keys and values from your text parser

Here’s the blueprint

blueprint (35).json (9.0 KB)

3 Likes

There is another approach using an iterator it turns out by using a little known toArray() trick on an array which changes the original array in a subtle but very useful way.

In the iterator you do toArray(itemOptions) …

image

This is done so that you get the following output bundles:

Each key value pair is now in its own collection called “Value”

Then in the text aggregator you can just dig into the current Value collection being iterated, using this expression:

{{first(keys(46.value))}}: {{get(46.value; keys(46.value))}}

The first(keys()) part part extracts the key name from the Value collection, then the : delimiter, and then you get the value part of the key/value by using the get() function.


Alex Sirota
Director of NewPath Consulting - we are :superhero: Make Heroes! :woman_superhero:t4:

:heart_on_fire: Check out my series of videos and scenario walkthroughs for Make Newbies :heart_on_fire:

My Solutions on Make Community

3 Likes

thanks so much @alex.newpath ! Does one of the solutions lend itself to accommodating more or less item options? Some may have 4, 5, 6, etc.

Yup both do. I like the second one better. It uses one less operation and doesn’t depend on a regular expression.

When you choose and approach mark the solution that worked for you best.

2 Likes

This worked! Thank you!!

2 Likes

Terrific! Enjoy your Make-ing. And thanks for marking the solution you used. I would have used that one too.

2 Likes