Foreach or maybe some other command for a Loop?

I am working on an API that will pull folder ID’s and totals from the response data. Essentially, trying to make a loop work to count all of the items in a folder, but it needs to drill down into subfolders to get an accurate count.

The response body is as follows:

{
    "data": [
        {
            "userId": 1416,
            "creator": {
                "id": 1416,
                "name": "Brandon Cox",
                "profileIcon": "42",
                "email": "brandon@kiteworks.net"
            },
            "maxFileLifetime": 0,
            "source": null,
            "isShared": true,
            "deleted": false,
            "secure": false,
            "type": "d",
            "name": "Finance Co LLC",
            "description": null,
            "parentId": 164070,
            "permalink": "https:",
            "maxFolderExpiration": 0,
            "created": "2023-04-11T14:33:12+0000",
            "modified": "2023-04-11T14:33:18+0000",
            "fileLifetime": 0,
            "path": "API Test Docs/Finance Co LLC",
            "permDeleted": false,
            "syncable": false,
            "id": 164153,
            "expire": 0
        },
        {
            "userId": 1416,
            "creator": {
                "id": 1416,
                "name": "Brandon Cox",
                "profileIcon": "42",
                "email": "brandon@kiteworks.net"
            },
            "maxFileLifetime": 0,
            "source": null,
            "isShared": true,
            "deleted": false,
            "secure": false,
            "type": "d",
            "name": "GHA Insurance",
            "description": null,
            "parentId": 164070,
            "permalink": "https://",
            "maxFolderExpiration": 0,
            "created": "2023-04-11T14:32:21+0000",
            "modified": "2023-04-11T14:32:27+0000",
            "fileLifetime": 0,
            "path": "API Test Docs/GHA Insurance",
            "permDeleted": false,
            "syncable": false,
            "id": 164120,
            "expire": 0
        },
        {
            "userId": 1416,
            "creator": {
                "id": 1416,
                "name": "Brandon Cox",
                "profileIcon": "42",
                "email": "brandon@kiteworks.net"
            },
            "maxFileLifetime": 0,
            "source": null,
            "isShared": true,
            "deleted": false,
            "secure": false,
            "type": "d",
            "name": "Legal House",
            "description": null,
            "parentId": 164070,
            "permalink": "https://",
            "maxFolderExpiration": 0,
            "created": "2023-04-11T14:32:58+0000",
            "modified": "2023-04-11T14:32:59+0000",
            "fileLifetime": 0,
            "path": "API Test Docs/Legal House",
            "permDeleted": false,
            "syncable": false,
            "id": 164142,
            "expire": 0
        },
        {
            "userId": 1416,
            "creator": {
                "id": 1416,
                "name": "Brandon Cox",
                "profileIcon": "42",
                "email": "brandon@kiteworks.net"
            },
            "maxFileLifetime": 0,
            "source": null,
            "isShared": true,
            "deleted": false,
            "secure": false,
            "type": "d",
            "name": "MFT_Test",
            "description": null,
            "parentId": 164070,
            "permalink": "https:1",
            "maxFolderExpiration": 0,
            "created": "2023-04-11T14:33:04+0000",
            "modified": "2023-04-11T14:33:05+0000",
            "fileLifetime": 0,
            "path": "API Test Docs/MFT_Test",
            "permDeleted": false,
            "syncable": false,
            "id": 164147,
            "expire": 0
        },
        {
            "userId": 1416,
            "creator": {
                "id": 1416,
                "name": "Brandon Cox",
                "profileIcon": "42",
                "email": "brandon@kiteworks.net"
            },
            "maxFileLifetime": 0,
            "source": null,
            "isShared": true,
            "deleted": false,
            "secure": false,
            "type": "d",
            "name": "New Hire Docs",
            "description": null,
            "parentId": 164070,
            "permalink": "https://",
            "maxFolderExpiration": 0,
            "created": "2023-04-11T14:33:19+0000",
            "modified": "2023-04-11T14:33:19+0000",
            "fileLifetime": 0,
            "path": "API Test Docs/New Hire Docs",
            "permDeleted": false,
            "syncable": false,
            "id": 164175,
            "expire": 0
        },
   
    ],
    "metadata": {
        "limit": null,
        "offset": null,
        "totalDeletedFilesCount": 0,
        "totalDeletedFoldersCount": 0,
        "total": 9
    }
}

So this is pulling info about the parent folder. The “total” at the bottom is the number I need to COUNT with, and each folder has an “id” that needs to be pulled for a loop so the folder can be looked into and have its “total” pulled.

I have zero idea of where to go for this. Any and all guidance is much appreciated!

Just to clarify, the number of objects in the data array, should equal the value in metadata.total?

If that’s the case, something basic like this would check that:

let response = pm.response.json();

console.log(response.data.length);

pm.test("number of objects to equal metadata total", () => {
    pm.expect(response.data.length).to.eql(response.metadata.total);
});

It uses .length on the array to count the number of objects.

Sort of. The value of “total” is correct. I just need to run a loop to get that number for each folder that is included in the response.

So, I am running an API call that checks the folder “id” and returns a list of files/folders saved to that folder. I need to:

  • Extract the “id” for each folder in the response above

  • Rerun the call with the extracted “id”

  • Get the “total” for each folder

I think maybe this is possible in a workflow or something, but I know there’s some Java that has to be added to run it correctly, and unfortunately I haven’t found good code for the use case.

Edit: the code I was using to set the folder ID’s is setting them manually (pm.collectionVariables.set(‘subfolder2’, response.data[0].id);
pm.collectionVariables.set(‘subfolder2’, response.data[1].id);). However, the issue if I run this on a different folder, I won’t know how many times to include that code with the response.data[whatever number].id piece. Big part of the ask for help.

Hey, I was able to figure this out! Thanks for the help!