Mapping Array to Array of Collection

Hello everyone,
I’m trying to achieve a rather simple thing but I’m not too sure how to go about it.


The scenario
My scenario is a simple setup where a Webhook is listening for incoming data and from which I want to create a variable (the complete scenario is more complex but I decided to scope the scenario for visualization sake).


The problem
I’m successfully getting the data from the Webhook. However, I’m having a hard time mapping it to the desire format.


In the screenshot below, I’m getting some data, notably an array of recipients.

From this array of recipients I’m trying to create a phoneNumbers variable with a specific format.
In fact I’m trying to create an array of Collection from these phone numbers and just set the key for each phone number to number like in the JSON example below.

    [
        {
            "number": "+250780123456"
        },
        {
            "number": "+250720654321"
        }
    ]

Right now I’m not sure what Variable value I should use in the Tools module so that the output bundle has the desired form. My understanding is the map function is more to filter out values and keys but not necessarily to modify the data structure.

Does anyone know how I can solve this?

1 Like

Hi @faranga
Download the blueprint attached
blueprint (89).json (7.8 KB)

Now in the create a json module click on add then generate the structure by just copying the following sample structure :

	{"recipients":[{"Number":"223"},{"Number":"+343535"},{"Number":"5234523"}]}

Click on generate and then click on save:

Simply click on save in create a Json module

Now in array aggregator select Target structure type as shown in image:

Now run to understand the logic.

Feel free to reach out if you have further queries

2 Likes

It worked! thank you so much

2 Likes

Glad to know your issue is solved. Feel free to DM if you have further queries

1 Like

Welcome to the Make community!

Here’s how to do it in a single module (and one operation), and no Data Structures.

You can also use this method with a Parse JSON module if you want it to be returned as an array variable.

Module Export - quick import into your scenario

You can copy and paste this module export into your scenario. This will import the modules (with fields/settings/filters) shown in my screenshots above.

  1. Move your mouse over the line of code below. Copy the JSON by clicking the copy button on the right of the code, which looks like this:

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the editor.

  3. Click on each imported module and re-save it for validation. There may be some errors prompting you to remap some variables and connections.

JSON module export — paste this directly in your scenario

{"subflows":[{"flow":[{"id":1,"module":"json:ParseJSON","version":1,"parameters":{"type":""},"mapper":{"json":"{\n  \"message\": \"This is a random message\",\n  \"country\": \"RW\",\n  \"recipients\": [\n    \"+250780123456\",\n    \"+250720654321\"\n  ]\n}"},"metadata":{"designer":{"x":0,"y":150},"parameters":[{"name":"type","type":"udt","label":"Data structure"}]}},{"id":2,"module":"builtin:BasicRouter","version":1,"mapper":null,"metadata":{"designer":{"x":300,"y":150,"name":"Run Examples"}},"routes":[{"flow":[{"id":3,"module":"json:ParseJSON","version":1,"parameters":{"type":""},"mapper":{"json":"{\"recipients\":[\n  {\"number\":\"{{join(1.recipients; \"\"\"},{\"\"number\"\":\"\"\")}}\"}\n]}"},"metadata":{"designer":{"x":600,"y":0,"name":"Output as array variable"},"parameters":[{"name":"type","type":"udt","label":"Data structure"}]}}]},{"flow":[{"id":4,"module":"util:ComposeTransformer","version":1,"parameters":{},"mapper":{"value":"{\"recipients\":[\n  {\"number\":\"{{join(1.recipients; \"\"\"},{\"\"number\"\":\"\"\")}}\"}\n]}"},"metadata":{"designer":{"x":600,"y":300,"name":"Output as JSON string"}}}]}]}]}],"metadata":{"version":1}}

Note: Did you know you can reduce the size of blueprints and module export code like the above, using the Make Blueprint Scrubber?

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

1 Like