Set variables from response array first element

My question:
I want to set the “approved_id” as a variable when “approved_by”=null, but I’ve already tried:

var jsonData = pm.response.json();
var data = jsonData.filter( e => e.approved_by ===“null”);
pm.collectionVariables.set(“Id”, data.approved_id);

“approved_id”: 1,
“approved_by”: null
“approved_id”: 2,
“approved_by”: “Tester”
"approved_id”: 3,
“approved_by”: null

filter returns an array. likely, you should use data[0].approved_id

when I use data[0].approved_id , will show “TypeError: Cannot read property ‘approved_id’ of undefined”, if use data.approved_id , the variable is empty

I think it could be because you are using “null”. It should be null. However, it returns 2 approved_id - 1 and 3 (in your examples). I believe you have the logic to choose which one of these you want to set as id.

1 Like

Thank you so much! solved!