How I can put Id of entry in variable by found name?

I need to take ID into the variable by found name. Like I have a lot of entries in the response, but I need to find certain name and then take the id of entry.

Body response looks like this
[
{
“ID”: 17,
“name”: “test”,
“type”: 1,
“perUnits”: 3,
“distanceThresholds”: [
3
],
“measurementThresholds”: [
3
],
“rates”: [
[
0
]
],
“size”: 35,
“breakpoints”: [
[
null
]
]
}
]

I found an entry name by this test:

pm.test(“Take Id by found name”, function () {
const findML = pm.environment.get(“Variable3”)
pm.expect(pm.response.text()).to.include(findML);
return console.log(findML)

@avionics-meteorolog6

Something along the lines of.

pm.test("Take Id by found name", () => {
    let search = (response.find(obj => {return obj.name === 'test'}));
    pm.expect(search).to.have.property('id');
    console.log(search.id); // you can store the result in a variable at this point
});
1 Like