Flatten Collection

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

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!

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), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.
Screenshot_2023-10-06_141025

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:

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

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

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!

2 Likes

blueprint (10).json (5.9 KB)
Here you go, thanks in advance!

[
    {
        "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
                }
            ]
        }
    }
]
[
    {
        "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

Welcome to the Make community!

You can use a Text Parser “Replace” module with this Pattern (regular expression):

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

Proof

https://regex101.com/r/QirD7V

Important Info

  • :warning: Global match must be set to YES!

Screenshot

Output


For more information, see 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 (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.

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

  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": 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
    }
}
3 Likes

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!

  1. Replace, corrected.

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

2 Likes

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

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

Error Message

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

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?

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

Screenshot_2024-02-29_170249

and the regex changes slightly:

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

Screenshot_2024-02-29_170243

2 Likes