Skip to content
How to flatten a co...
 
Notifications
Clear all

How to flatten a collection

10 Posts
2 Users
0 Reactions
4 Views
Gragamel-Pow
(@gragamel-pow)
Posts: 6
Active Member
Topic starter
 

Dear callin.io Community,

I'm looking to "flatten" this collection, and I know it's possible with arrays. I was wondering if there's a workaround to achieve the same with collections.

I have an Airtable API Call that outputs the following data structure:

Screenshot 2024-02-27 at 12.32.44

In the next step, I'm transforming this into JSON using the following formula:

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

This results in my JSON not including the "id" or "createdTime".

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

Thanks in advance!

 
Posted : 27/02/2024 11:54 am
samliew
(@samliew)
Posts: 293
Reputable Member
 

Welcome to the callin.io 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!

 
Posted : 27/02/2024 2:59 pm
Gragamel-Pow
(@gragamel-pow)
Posts: 6
Active Member
Topic starter
 

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

 
Posted : 28/02/2024 9:12 am
Gragamel-Pow
(@gragamel-pow)
Posts: 6
Active Member
Topic starter
 

Here's the JSON output I'm aiming for after transforming my data. I need to exclude the fields array from my blueprint.

[
    {
        "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}}]"
    }
]
 
Posted : 28/02/2024 9:20 am
samliew
(@samliew)
Posts: 293
Reputable Member
 

Welcome to the callin.io community!

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

"fields":s+{(?<fields>[wW]+?)}

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 callin.io 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>[wW]+?)}",
                        "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": truen                }n            },n            {n                "id": "rec123XYZ",n                "createdTime": "2024-09-07T12:00:00.000Z",n                "fields": {n                    "fields1": "abc",n                    "fields2": falsen                }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
    }
}
 
Posted : 28/02/2024 9:37 am
Gragamel-Pow
(@gragamel-pow)
Posts: 6
Active Member
Topic starter
 

Thanks for the follow-up.

  1. You mentioned "Match pattern," but the module you're utilizing is "Replace." Could you clarify which one is accurate?

  2. As I need to make an API call beforehand to retrieve the data, the body is a collection. Your replace module doesn't support collections because it requires text input. Would you mind trying again with a "Parse JSON" module first to ensure it's simulated correctly?

Thanks a lot!

 
Posted : 28/02/2024 9:46 am
samliew
(@samliew)
Posts: 293
Reputable Member
 
  1. Replace, corrected.

  2. 2.

You can disable “Parse Response” within the HTTP module to receive the JSON output instead.

 
Posted : 28/02/2024 9:49 am
Gragamel-Pow
(@gragamel-pow)
Posts: 6
Active Member
Topic starter
 

It appears the issue isn't with the http module, but rather with the Airtable API call module.

Is there an alternative solution for this?

Additionally, the Transform JSON module is not functioning correctly with the output from the regex module.

Transform JSON input

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

Error Message

DataError
Failed to map 'object': Function 'map' finished with error! Invalid array.
 
Posted : 28/02/2024 9:51 am
Gragamel-Pow
(@gragamel-pow)
Posts: 6
Active Member
Topic starter
 

I've done a bit more testing and realized I need the id and createdTime parameters within the fields array. This will allow me to map them correctly and obtain an array for use with my transform to JSON module.

Is there a way to include these parameters in an array?

 
Posted : 28/02/2024 10:07 am
samliew
(@samliew)
Posts: 293
Reputable Member
 

In that scenario, you can simply apply the “Transform to JSON” module to the body first.

Screenshot_2024-02-29_170249

And the regex requires a minor adjustment:

"fields":{(?&lt;fields&gt;[wW]+?)}

Screenshot_2024-02-29_170243

 
Posted : 29/02/2024 9:26 am
Share: