# Foreach or maybe some other command for a Loop?

**URL:** <https://community.postman.com/t/foreach-or-maybe-some-other-command-for-a-loop/46256>\
**Category:** Help Hub\
**Tags:** variables, loop, iterations, loops\
**Created:** [April 12, 2023, 1:22pm UTC](https://community.postman.com/t/foreach-or-maybe-some-other-command-for-a-loop/46256 "2023-04-12T13:22:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brcox9090](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@brcox9090](https://community.postman.com/u/brcox9090)\
**Post date:** [April 12, 2023, 1:22pm UTC](https://community.postman.com/t/foreach-or-maybe-some-other-command-for-a-loop/46256/1 "2023-04-12T13:22:01Z")

</div>

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:

```auto
{
    "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!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 12, 2023, 2:01pm UTC](https://community.postman.com/t/foreach-or-maybe-some-other-command-for-a-loop/46256/2 "2023-04-12T14:01:05Z")

</div>

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:

```auto
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) on the array to count the number of objects.

---

<div class="post-metadata">

**Author:** ![brcox9090](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@brcox9090](https://community.postman.com/u/brcox9090)\
**Post date:** [April 12, 2023, 5:05pm UTC](https://community.postman.com/t/foreach-or-maybe-some-other-command-for-a-loop/46256/3 "2023-04-12T17:05:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brcox9090](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@brcox9090](https://community.postman.com/u/brcox9090)\
**Post date:** [April 12, 2023, 9:01pm UTC](https://community.postman.com/t/foreach-or-maybe-some-other-command-for-a-loop/46256/4 "2023-04-12T21:01:18Z")

</div>

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