# Flatten Collection

**URL:** <https://community.make.com/t/flatten-collection/28793>\
**Category:** Questions\
**Tags:** arrays, functions, mapping\
**Created:** [February 27, 2024, 11:54am UTC](https://community.make.com/t/flatten-collection/28793 "2024-02-27T11:54:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gragamel-Pow](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/gragamel-pow/32/23686_2.png) [@Gragamel-Pow](https://community.make.com/u/Gragamel-Pow)\
**Post date:** [February 27, 2024, 11:54am UTC](https://community.make.com/t/flatten-collection/28793/1 "2024-02-27T11:54:47Z")

</div>

Dear Make Community,

I would like to “flatten” this collection if possible, I know its doable with arrays and was curious if there is a workaround to do the same with collections

I have an Airtable API Call that outputs the following datastructure

![Screenshot 2024-02-27 at 12.32.44](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/3/8/38d7ac63ceb704e316cad1f585a6880b3645d3fb.png)

In the next step, I am transforming this into JSON with the following formula

> {{map(records; “fields”)}}

This leads to my JSON not containing the “id” or “createdTime”

What can I do to either lower these a level into the fields collection or include them all in my JSON string?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![samliew](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/samliew/32/13327_2.png) [@samliew](https://community.make.com/u/samliew)\
**Post date:** [February 27, 2024, 2:59pm UTC](https://community.make.com/t/flatten-collection/28793/2 "2024-02-27T14:59:54Z")

</div>

Welcome to the Make community!

Please provide the **output** bundles of the above modules by running the scenario (or get from the [scenario History tab](https://www.make.com/en/help/scenarios/scenario-execution-history)), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

 ![Screenshot_2023-10-06_141025](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/2/8/2876156ab3ea5bc9e40bbc0edfb9f025406b7c48.png)

## A.

Save each bundle contents in your text editor as a `bundle.txt` file, and upload it here into this discussion thread.

Uploading them here will look like this:

> module-1-input-bundle.txt (12.3 KB)  
> module-1-output-bundle.txt (12.3 KB)

## B.

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

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

- Or use the format code button in the editor:  
 ![Screenshot_2023-10-02_191027](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/b/4/b485b6572c7a1c438f9b191c54e63d351008304c.png)

Providing the input/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!

---

<div class="post-metadata">

**Author:** ![Gragamel-Pow](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/gragamel-pow/32/23686_2.png) [@Gragamel-Pow](https://community.make.com/u/Gragamel-Pow)\
**Post date:** [February 28, 2024, 9:12am UTC](https://community.make.com/t/flatten-collection/28793/3 "2024-02-28T09:12:49Z")

</div>

[blueprint (10).json](https://community.make.com/uploads/short-url/qfyHjuJ7iBRoQ0muERJR8Ua3BoC.json) (5.9 KB)  
Here you go, thanks in advance!

---

<div class="post-metadata">

**Author:** ![Gragamel-Pow](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/gragamel-pow/32/23686_2.png) [@Gragamel-Pow](https://community.make.com/u/Gragamel-Pow)\
**Post date:** [February 28, 2024, 9:20am UTC](https://community.make.com/t/flatten-collection/28793/4 "2024-02-28T09:20:16Z")

</div>

```auto
[
    {
        "body": {
            "records": [
                {
                    "id": "recrsBYQ",
                    "createdTime": "2024-09-06T10:22:21.000Z",
                    "fields1": "xyz",
                    "fields2": true
                },
                {
                    "id": "rec123XYZ",
                    "createdTime": "2024-09-07T12:00:00.000Z",
                    "fields1": "abc",
                    "fields2": false
                }
            ]
        }
    }
]

```

```auto
[
    {
        "json": "[{\"id\":\"recrsBYQ\",\"createdTime\":\"2024-09-06T10:22:21.000Z\",\"fields1\":\"xyz\",\"fields2\":true},{\"id\":\"rec123XYZ\",\"createdTime\":\"2024-09-07T12:00:00.000Z\",\"fields1\":\"abc\",\"fields2\":false}]"
    }
]

```

this is my desired output of the transform to json file and to that i have to remove the “fields” array seen in my blueprint

---

<div class="post-metadata">

**Author:** ![samliew](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/samliew/32/13327_2.png) [@samliew](https://community.make.com/u/samliew)\
**Post date:** [February 28, 2024, 9:37am UTC](https://community.make.com/t/flatten-collection/28793/5 "2024-02-28T09:37:29Z")

</div>

Welcome to the Make community!

You can use a Text Parser “[Replace](https://www.make.com/en/help/tools/text-parser)” module with this Pattern (regular expression):

```plaintext
"fields":\s+{(?<fields>[\w\W]+?)}

```

# Proof

[https://regex101.com/r/QirD7V](https://regex101.com/r/QirD7V)

### Important Info

- ⚠ **Global match** must be set to YES!

# Screenshot

 ![Screenshot_2024-02-28_203622](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/9/d/9db31bedcfe01479449f53065fa01110e1313e07.png)

# Output

 ![Screenshot_2024-02-28_203713](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/8/2/821ff454496077ed68dfe76f5bbd3d503273a75e.png)

* * *

For more information, see [Text Parser](https://www.make.com/en/help/tools/text-parser) in the Make Help Center:

> **Match Pattern**  
> The **Match pattern** module enables you to find and extract string elements matching a search pattern from a given text. The search pattern is a [regular expression](https://en.wikipedia.org/wiki/Regular_expression) (aka regex or regexp), which is a sequence of characters in which each character is either a metacharacter, having a special meaning, or a regular character that has a literal meaning.
> 
> - The complete list of metacharacters can be found on the [MDN web docs website](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions).
> - For a tutorial on how to create regular expressions, we recommend the [RegexOne website](https://regexone.com/).
> - For an easy, quick regex generator, try the [Regular Expressions generator](https://regex-generator.olafneumann.org/).
> - For experimenting with regular expressions, we recommend the [regular expressions 101 website](https://regex101.com/). Just make sure to tick the ECMAScript (JavaScript) FLAVOR in the left panel.

Hope this helps!

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](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/2/b/2b1b692880388420db5045bf51ef344e6698a816.png)

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

```json
{
    "subflows": [
        {
            "flow": [
                {
                    "id": 5,
                    "module": "regexp:Replace",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "pattern": "\"fields\":\\s+{(?<fields>[\\w\\W]+?)}",
                        "value": "$1",
                        "global": true,
                        "sensitive": true,
                        "multiline": false,
                        "singleline": false,
                        "text": "{\n \"body\": {\n \"records\": [\n {\n \"id\": \"recrsBYQ\",\n \"createdTime\": \"2024-09-06T10:22:21.000Z\",\n \"fields\": {\n \"fields1\": \"xyz\",\n \"fields2\": true\n }\n },\n {\n \"id\": \"rec123XYZ\",\n \"createdTime\": \"2024-09-07T12:00:00.000Z\",\n \"fields\": {\n \"fields1\": \"abc\",\n \"fields2\": false\n }\n }\n]\n }\n}\n​"
                    },
                    "metadata": {
                        "designer": {
                            "x": -21,
                            "y": 398,
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {
                            "expect": {
                                "global": {
                                    "mode": "chose"
                                },
                                "sensitive": {
                                    "mode": "chose"
                                },
                                "multiline": {
                                    "mode": "chose"
                                },
                                "singleline": {
                                    "mode": "chose"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "pattern",
                                "type": "text",
                                "label": "Pattern",
                                "required": true
                            },
                            {
                                "name": "value",
                                "type": "text",
                                "label": "New value"
                            },
                            {
                                "name": "global",
                                "type": "boolean",
                                "label": "Global match",
                                "required": true
                            },
                            {
                                "name": "sensitive",
                                "type": "boolean",
                                "label": "Case sensitive",
                                "required": true
                            },
                            {
                                "name": "multiline",
                                "type": "boolean",
                                "label": "Multiline",
                                "required": true
                            },
                            {
                                "name": "singleline",
                                "type": "boolean",
                                "label": "Singleline",
                                "required": true
                            },
                            {
                                "name": "text",
                                "type": "text",
                                "label": "Text"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

```

---

<div class="post-metadata">

**Author:** ![Gragamel-Pow](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/gragamel-pow/32/23686_2.png) [@Gragamel-Pow](https://community.make.com/u/Gragamel-Pow)\
**Post date:** [February 28, 2024, 9:46am UTC](https://community.make.com/t/flatten-collection/28793/6 "2024-02-28T09:46:59Z")

</div>

Thanks for getting back to me.

1. You are saying “Match pattern” but the module you use is “Replace” - which one is it?

2. Since I have to make an API call in advance to get the data, the body is a collection and your replace module does not work with collections since it needs a text. Can you please try again with a “Parse JSON” module before so it is simulated correctly?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![samliew](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/samliew/32/13327_2.png) [@samliew](https://community.make.com/u/samliew)\
**Post date:** [February 28, 2024, 9:49am UTC](https://community.make.com/t/flatten-collection/28793/7 "2024-02-28T09:49:15Z")

</div>

1. Replace, corrected.

2. You can turn off “Parse Response” in the HTTP module, to get the JSON instead.

---

<div class="post-metadata">

**Author:** ![Gragamel-Pow](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/gragamel-pow/32/23686_2.png) [@Gragamel-Pow](https://community.make.com/u/Gragamel-Pow)\
**Post date:** [February 28, 2024, 9:51am UTC](https://community.make.com/t/flatten-collection/28793/8 "2024-02-28T09:51:41Z")

</div>

It is unfortunately not the http module but rather the airtable - api call module

is there another solution to this?

also the transform to json module is not working unfortunately with the output of the regex module

Transform JSON input

```auto
{{map(15.text; "records")}}

```

Error Message

```auto
DataError
Failed to map 'object': Function 'map' finished with error! Invalid array.

```

---

<div class="post-metadata">

**Author:** ![Gragamel-Pow](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/gragamel-pow/32/23686_2.png) [@Gragamel-Pow](https://community.make.com/u/Gragamel-Pow)\
**Post date:** [February 28, 2024, 10:07am UTC](https://community.make.com/t/flatten-collection/28793/9 "2024-02-28T10:07:10Z")

</div>

i just test around a little bit more and i think i need the id and createdTime parameters inside of the fields array to that i can map this and get a correct array to use for my transfrom to json module

is there any solution to add these into an array?

---

<div class="post-metadata">

**Author:** ![samliew](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/samliew/32/13327_2.png) [@samliew](https://community.make.com/u/samliew)\
**Post date:** [February 29, 2024, 9:26am UTC](https://community.make.com/t/flatten-collection/28793/10 "2024-02-29T09:26:19Z")

</div>

> [@Gragamel-Pow](#):
>
> It is unfortunately not the http module but rather the airtable - api call module

In that case, you can just use the “Transform to JSON” module on the body first.

![Screenshot_2024-02-29_170249](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/e/d/edc5e60795ec19311a735c85b60ad074c698059c.png)

and the [regex](https://regex101.com/r/evzUfg) changes slightly:

`"fields":{(?<fields>[\w\W]+?)}`

![Screenshot_2024-02-29_170243](https://europe1.discourse-cdn.com/flex013/uploads/make/original/3X/2/e/2e18f8d8baf762b06ce5724fee23713428b14d57.png)

---

<div class="post-metadata">

**Author:** ![Make\_Bot](https://dub1.discourse-cdn.com/flex013/user_avatar/community.make.com/make_bot/32/14661_2.png) [@Make\_Bot](https://community.make.com/u/Make_Bot)\
**Post date:** [May 29, 2024, 8:34am UTC](https://community.make.com/t/flatten-collection/28793/11 "2024-05-29T08:34:09Z")

</div>


