Construct folder path by concatenating items from path_collection

I have an API response that looks something like this:

    "type": "folder",
    "id": "1234567890",
    "etag": "0",
    "name": "Folder2",
    "created_at": "2021-02-09T08:15:15-08:00",
    "modified_at": "2021-08-04T13:32:40-07:00",
    "description": "",
    "size": 2931028,
    "path_collection": {
        "total_count": 3,
        "entries": [
                "type": "folder",
                "id": "0",
                "sequence_id": null,
                "etag": null,
                "name": "All Files"
                "type": "folder",
                "id": "1808778217",
                "sequence_id": "0",
                "etag": "0",
                "name": "Users"
                "type": "folder",
                "id": "1869977869",
                "sequence_id": "0",
                "etag": "0",
                "name": "Folder1"

I want to use the “name” values within the path_collection block to piece together a folder path that would look something like this:

/All Files/Users/Folder1/Folder2

I was hoping to achieve this using a Postman test script. Or would it be better to use a pre-request script? Can anyone assist me with the JavaScript that would be needed in order to achieve this objective?

This solution is very close to what I hope to achieve; but the syntax is not quite right.

Hey @anengelsen :wave: Welcome to the Community :trophy:

As you showed in the link, we just have to loop through the response and get the folder name and append it to a string, one way you can do it is:

var response = pm.resonse.json()
var pathCollection = response.path_collection
var finalPath="/"


console.log(finalPath.slice(0, -1))  //depending on the last item, if it is a folder you need not remove the last '/' 

This is just the basic idea of looping and fetching the Folder/File name, you can modify according to your needs.

Hope this helps,
Good luck!

Thanks @bbahadur,
This helps a lot. However, when I run the test script, I’m currently getting an error message that says: TypeError: Cannot read property 'json' of undefined

Why is that?

Haha. Problem solved. I just needed to provide Postman with the correct spelling of the word “response”.

var response = pm.response.json() vs. var response = pm.resonse.json()

Thanks again for your help @bbahadur!

1 Like

Ah! :stuck_out_tongue: My bad, I missed the spelling while typing.
Great that you figured it out and it worked for you!