Fetch response parameter value based on another parameter value

I want to store the parameter value based on another parameter value

[
{
“event_category”: “a”,
“subscription_id”: “73303981”
},
{
“event_category”: “b”,
“subscription_id”: “e58432e1”
},
{
“event_category”: “c”,
“subscription_id”: “911adc36”
}
]

From the above array, need to get the subscription_id for particular event_category and store the value in Postman environment variable . How i can do that ?

Something like the following should achieve your goal.

const response = pm.response.json();

let event_category = "b";

let search = (response.find(obj => {return obj.event_category === event_category}));
console.log(search);

pm.test(`event category ${event_category} exists and is not null`, () => {
    pm.expect(search).to.have.property('subscription_id').and.to.not.equal(null).and.not.be.undefined;
    pm.environment.set("environment_variable", search.subscription_id)
});

Put the setting of the environment variable in a test (so its only gets set if the test passes).

You can just target the subscription_id directly using the following (but I would recommend the first approach).

const response = pm.response.json();

let event_category = "b";

let search = (response.find(obj => {return obj.event_category === event_category})).subscription_id;
console.log(search);`
1 Like

Thank u so much @mdjones . It worked.