Mapping Data Is Producing [collection]

I figured I might as well add my post to the ever-growing list of unsolved posts about this :smiling_face_with_tear:

I need to replicate this code in Make

const blockContents = blocks
  .filter((block) => block.type !== "file")
  .map((block) => {
    return {
      type: block.type,
      [block.type]: {
        ...block[block.type],
      },
    };
  });

So essentially, extracting

                {
                    "paragraph": {
                        "rich_text": [
                            {
                                "type": "text",
                                "text": {
                                    "content": "Some text",
                                    "link": null
                                },
                                "annotations": {
                                    "bold": false,
                                    "italic": false,
                                    "strikethrough": false,
                                    "underline": false,
                                    "code": false,
                                    "color": "orange_background"
                                },
                                "plain_text": "Some text",
                                "href": null
                            }
                        ],
                        "color": "default"
                    }
                }

from objects like this

                {
                    "object": "block",
                    "id": "2e086381-b461-4860-8ab5-9279c992f78d",
                    "parent": {
                        "type": "page_id",
                        "page_id": "ba4aca1a-4a13-4e23-adc0-0f8d7d767b94"
                    },
                    "created_time": "2024-01-09T13:53:00.000Z",
                    "last_edited_time": "2024-01-09T14:17:00.000Z",
                    "created_by": {
                        "object": "user",
                        "id": "76c0e327-b5c1-4c38-b0e9-73afdbbff391"
                    },
                    "last_edited_by": {
                        "object": "user",
                        "id": "76c0e327-b5c1-4c38-b0e9-73afdbbff391"
                    },
                    "has_children": false,
                    "archived": false,
                    "in_trash": false,
                    "type": "paragraph",
                    "paragraph": {
                        "rich_text": [
                            {
                                "type": "text",
                                "text": {
                                    "content": "Some text",
                                    "link": null
                                },
                                "annotations": {
                                    "bold": false,
                                    "italic": false,
                                    "strikethrough": false,
                                    "underline": false,
                                    "code": false,
                                    "color": "orange_background"
                                },
                                "plain_text": "Some text",
                                "href": null
                            }
                        ],
                        "color": "default"
                    }
                }

I do need the entire collection because I’m copying these blocks (without their IDs), from one Notion page to another. So I need to insert the collection in another API request.

But Make gives me

{
    "type": "heading_3",
    "heading_3": "[Collection]"
}

here

Try the pick() function which works on collections

{{pick(objectname;paragraph)}}

1 Like

Thanks but I’m not sure how to implement that here, I’ve tried wrapping

{{get(6.body.results; 4.IMTINDEX; 4.type)}}

in the pick function like this

{ "type": "{{4.type}}", "{{4.type}}": {{pick(get(6.body.results; 4.IMTINDEX; 4.type); "paragraph")}} }

but that doesn’t seem to have any effect.

Here’s a blueprint of the scenario if that helps?

Fetch Page Blocks Demo.json (12.2 KB)

Hi again,

I loaded up your full object into a Parse JSON module and realized that the paragraph collection needs no function to access it – you simply reference it as follows

{{1.paragraph}}

Since there are no arrays and it’s just a set of nested collections the paragraph collection is at the top level of the bundle.

But your question is really more about iterating the results array coming out of an HTTP request so it’s a bit more complex than that.

Since you’re forming a new piece of JSON text, I use the Transform to JSON module to convert your heading_2 and paragraph collections into JSON text in the iterator loop. Then in the aggregator I grab the relevant object based on the bundle order position. The text aggregator expects text to aggregate so you cannot reference the objects there or you’ll just get {object} or {collection} as the output and you need actual text to place into your resulting new structure. Then I do a final Parse JSON so you can confirm indeed you are getting the results you need.

blueprint (39).json (23.9 KB)

But you can also do this without iterators or any of the other modules in just 1 set multiple variables module:

to get heading_2 you do

{{remove(map(6.body.results; "heading_2"); null)}}

to get paragraph(s) you do

{{remove(map(6.body.results; "paragraph"); null)}}

The remove call removes the empty collections generated as a result of the map to the results array which has 3 elements in it and the map for heading_2 generates 2 empty collections, while paragraph generates 1 empty collection since there are 2 paragraph collections in the array.

You can even merge these into one set of collections with the merge() function

{{merge(remove(map(6.body.results; "heading_2"); null); remove(map(6.body.results; "paragraph"); null))}}

copy and paste this blueprint using map() into a new scenario
{
    "subflows": [
        {
            "flow": [
                {
                    "id": 6,
                    "module": "json:ParseJSON",
                    "version": 1,
                    "parameters": {
                        "type": ""
                    },
                    "mapper": {
                        "json": "[\n    {\n        \"body\": {\n            \"object\": \"list\",\n            \"results\": [\n                {\n                    \"object\": \"block\",\n                    \"id\": \"0825bcb9-af91-4a0d-bacb-9121a8134aa3\",\n                    \"parent\": {\n                        \"type\": \"page_id\",\n                        \"page_id\": \"5488ff58-8374-4f31-8b89-4b470461f897\"\n                    },\n                    \"created_time\": \"2024-06-07T10:12:00.000Z\",\n                    \"last_edited_time\": \"2024-06-07T10:12:00.000Z\",\n                    \"created_by\": {\n                        \"object\": \"user\",\n                        \"id\": \"7a0fb8cf-d3c5-452d-b159-446a826bae3d\"\n                    },\n                    \"last_edited_by\": {\n                        \"object\": \"user\",\n                        \"id\": \"7a0fb8cf-d3c5-452d-b159-446a826bae3d\"\n                    },\n                    \"has_children\": false,\n                    \"archived\": false,\n                    \"in_trash\": false,\n                    \"type\": \"heading_2\",\n                    \"heading_2\": {\n                        \"rich_text\": [\n                            {\n                                \"type\": \"text\",\n                                \"text\": {\n                                    \"content\": \"A Heading\",\n                                    \"link\": null\n                                },\n                                \"annotations\": {\n                                    \"bold\": false,\n                                    \"italic\": false,\n                                    \"strikethrough\": false,\n                                    \"underline\": false,\n                                    \"code\": false,\n                                    \"color\": \"default\"\n                                },\n                                \"plain_text\": \"A Heading\",\n                                \"href\": null\n                            }\n                        ],\n                        \"is_toggleable\": false,\n                        \"color\": \"default\"\n                    }\n                },\n                {\n                    \"object\": \"block\",\n                    \"id\": \"9abed4e9-7e71-499e-8e65-485460bf213a\",\n                    \"parent\": {\n                        \"type\": \"page_id\",\n                        \"page_id\": \"5488ff58-8374-4f31-8b89-4b470461f897\"\n                    },\n                    \"created_time\": \"2024-06-07T10:12:00.000Z\",\n                    \"last_edited_time\": \"2024-06-07T10:12:00.000Z\",\n                    \"created_by\": {\n                        \"object\": \"user\",\n                        \"id\": \"7a0fb8cf-d3c5-452d-b159-446a826bae3d\"\n                    },\n                    \"last_edited_by\": {\n                        \"object\": \"user\",\n                        \"id\": \"7a0fb8cf-d3c5-452d-b159-446a826bae3d\"\n                    },\n                    \"has_children\": false,\n                    \"archived\": false,\n                    \"in_trash\": false,\n                    \"type\": \"paragraph\",\n                    \"paragraph\": {\n                        \"rich_text\": [\n                            {\n                                \"type\": \"text\",\n                                \"text\": {\n                                    \"content\": \"Some text.\",\n                                    \"link\": null\n                                },\n                                \"annotations\": {\n                                    \"bold\": false,\n                                    \"italic\": false,\n                                    \"strikethrough\": false,\n                                    \"underline\": false,\n                                    \"code\": false,\n                                    \"color\": \"default\"\n                                },\n                                \"plain_text\": \"Some text.\",\n                                \"href\": null\n                            }\n                        ],\n                        \"color\": \"default\"\n                    }\n                },\n                {\n                    \"object\": \"block\",\n                    \"id\": \"c25888a9-281b-4355-b370-b43d13fa4fee\",\n                    \"parent\": {\n                        \"type\": \"page_id\",\n                        \"page_id\": \"5488ff58-8374-4f31-8b89-4b470461f897\"\n                    },\n                    \"created_time\": \"2024-06-07T10:12:00.000Z\",\n                    \"last_edited_time\": \"2024-06-07T10:13:00.000Z\",\n                    \"created_by\": {\n                        \"object\": \"user\",\n                        \"id\": \"7a0fb8cf-d3c5-452d-b159-446a826bae3d\"\n                    },\n                    \"last_edited_by\": {\n                        \"object\": \"user\",\n                        \"id\": \"7a0fb8cf-d3c5-452d-b159-446a826bae3d\"\n                    },\n                    \"has_children\": false,\n                    \"archived\": false,\n                    \"in_trash\": false,\n                    \"type\": \"paragraph\",\n                    \"paragraph\": {\n                        \"rich_text\": [\n                            {\n                                \"type\": \"text\",\n                                \"text\": {\n                                    \"content\": \"Some more text.\",\n                                    \"link\": null\n                                },\n                                \"annotations\": {\n                                    \"bold\": false,\n                                    \"italic\": false,\n                                    \"strikethrough\": false,\n                                    \"underline\": false,\n                                    \"code\": false,\n                                    \"color\": \"default\"\n                                },\n                                \"plain_text\": \"Some more text.\",\n                                \"href\": null\n                            }\n                        ],\n                        \"color\": \"default\"\n                    }\n                }\n            ],\n            \"next_cursor\": null,\n            \"has_more\": false,\n            \"type\": \"block\",\n            \"block\": {},\n            \"request_id\": \"478c8e1d-4b8c-4c38-a2ae-4a6373796805\"\n        },\n        \"headers\": {\n            \"date\": \"Fri, 07 Jun 2024 10:13:23 GMT\",\n            \"content-type\": \"application/json; charset=utf-8\",\n            \"transfer-encoding\": \"chunked\",\n            \"connection\": \"close\",\n            \"x-powered-by\": \"Express\",\n            \"x-notion-request-id\": \"478c8e1d-4b8c-4c38-a2ae-4a6373796805\",\n            \"etag\": \"W/\\\"90e-faeK4/3nYw0G1YL7nKnhLeqmwEM\\\"\",\n            \"vary\": \"Accept-Encoding\",\n            \"cf-cache-status\": \"DYNAMIC\",\n            \"set-cookie\": [\n                \"__cf_bm=qezvWFcp5sErSCpQVqmaJsQ.q.00qE_94aeyt1GsbV8-1717755203-1.0.1.1-5ck9n4rVMRAa6XXcef0zMl8VRE6GOcXMrlOHhL6RM5UWvbw0pxSErwn8Bu9Bxs61uJNAxG6.gXVyAjONR2IveA; path=/; expires=Fri, 07-Jun-24 10:43:23 GMT; domain=.notion.com; HttpOnly; Secure; SameSite=None\"\n            ],\n            \"server\": \"cloudflare\",\n            \"cf-ray\": \"88ffde869fd59573-DUB\"\n        },\n        \"statusCode\": 200\n    }\n]"
                    },
                    "metadata": {
                        "designer": {
                            "x": 0,
                            "y": 150
                        },
                        "restore": {
                            "parameters": {
                                "type": {
                                    "label": "Choose a data structure"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "type",
                                "type": "udt",
                                "label": "Data structure"
                            }
                        ],
                        "expect": [
                            {
                                "name": "json",
                                "type": "text",
                                "label": "JSON string",
                                "required": true
                            }
                        ]
                    }
                },
                {
                    "id": 10,
                    "module": "builtin:BasicRouter",
                    "version": 1,
                    "mapper": null,
                    "metadata": {
                        "designer": {
                            "x": 300,
                            "y": 150
                        }
                    },
                    "routes": [
                        {
                            "flow": [
                                {
                                    "id": 11,
                                    "module": "util:SetVariables",
                                    "version": 1,
                                    "parameters": {},
                                    "mapper": {
                                        "variables": [
                                            {
                                                "name": "heading_2",
                                                "value": "{{remove(map(6.body.results; \"heading_2\"); null)}}"
                                            },
                                            {
                                                "name": "paragraph",
                                                "value": "{{remove(map(6.body.results; \"paragraph\"); null)}}"
                                            }
                                        ],
                                        "scope": "roundtrip"
                                    },
                                    "metadata": {
                                        "designer": {
                                            "x": 600,
                                            "y": 300
                                        },
                                        "restore": {
                                            "expect": {
                                                "variables": {
                                                    "items": [
                                                        null,
                                                        null
                                                    ]
                                                },
                                                "scope": {
                                                    "label": "One cycle"
                                                }
                                            }
                                        },
                                        "expect": [
                                            {
                                                "name": "variables",
                                                "type": "array",
                                                "label": "Variables",
                                                "spec": [
                                                    {
                                                        "name": "name",
                                                        "label": "Variable name",
                                                        "type": "text",
                                                        "required": true
                                                    },
                                                    {
                                                        "name": "value",
                                                        "label": "Variable value",
                                                        "type": "any"
                                                    }
                                                ]
                                            },
                                            {
                                                "name": "scope",
                                                "type": "select",
                                                "label": "Variable lifetime",
                                                "required": true,
                                                "validate": {
                                                    "enum": [
                                                        "roundtrip",
                                                        "execution"
                                                    ]
                                                }
                                            }
                                        ],
                                        "interface": [
                                            {
                                                "name": "heading_2",
                                                "label": "heading_2",
                                                "type": "any"
                                            },
                                            {
                                                "name": "paragraph",
                                                "label": "paragraph",
                                                "type": "any"
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

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

These scenario walkthroughs can be very useful for people new to Make and need a quick set of exercises to manipulate data structures.


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

4 Likes

This is great, thanks so much for the detailed answer! The second solution -

Fetch Page Blocks Solution 2.json (11.8 KB)

is basically what I need, in terms of extracting the blocks content. The only thing is, since I’m copying the contents of one page to another, I need to end up with an array of blocks, with each block in same order as they were on the original page. So:

Heading
Paragraph
Paragraph
Heading
Paragraph
Etc.

Is there an aggregator equivalent of the set variable step, that I can use to extract the blocks and combine them into a list? I’d like to copy these blocks from the page every time the automation runs and a page could contain 40+ blocks so I need to avoid processing each block 1 by 1 here…

If you have a variable number of blocks in the result array you will need to iterate and aggregate them into an array and then transform the resulting structure to json like in the first solution.

There is no way to do dynamic mapping based on a variable number of collections in an array so the iteration is required.

1 Like

Thanks for letting me know, it’s great to have confirmation that this is a limitation. Code it is then :sweat_smile:

It’s not code per se, but the fact that you have interspersed headings with paragraphs in result array, makes it impossible to preserve the original order of these collections. Each map() grabs the collections you specify, and if you merge them together you’ll get the header_2’s first and paragraph’s next and lose the original order. That’s why iteration is important to grab the element you need, and the first solution I suggested is probably the better approach (using transform to json inside the iterator).

2 Likes