Filter function with postman response

I am using Postman to capture some attributes from a response.
Response is as below:

{
    "data": [
        {
            "id": "ws-abc",
            "type": "workspaces",
            "attributes": {
                "environment": "default",
                "locked": false,
                "name": "Practice"
            }
        },
        {
			"id": "ws-xyz",
            "type": "workspaces",
            "attributes": {
                "environment": "default",
                "locked": false,
            }   "name": "Practice-1"
        },
		{
			"id": "ws-xyz",
            "type": "workspaces",
            "attributes": {
                "environment": "default",
                "locked": false,
                "name": "Practice-2"
			}
	    }
	]
}

I want to extract id (line #4) for the workspace named Practice (line #9).

I am able to do that using below code, but I also want to use the name filtering.

const response = pm.response.json();
console.log(response.data[1].id)

When I include the below variable, I am getting an error:

const workspaceName = response.filter((ws) => ws.data.attributes.name === "Practice");
TypeError: response.filter is not a function

I understand the filter function can only be used with arrays, and my response is not an array.

How to I achieve this ? I am new to Postman and JavaScript and appreciate your help!

1 Like

Hey @jijo333

It looks like you would need to do something like this to filter the data array:

const workspaceName = response.data.filter(ws => ws.attributes.name === "Practice");
3 Likes

Thank you @dannydainton , that solved the problem!

1 Like