Home > Mobile >  Postman: filter nested response
Postman: filter nested response

Time:04-28

Im trying postman. I have request , which just returns json like

    {
       "people":[
          {
             "name":"Joe",
             "nationality":"GBR"
          },
          {
             "name":"Ben",
             "nationality":"USA"
          },
          {
             "name":"Ben",
             "nationality":"NOR"
          }
       ]
    }

Goal: add test to postman, which will parse this response, and set environment property like "nationality of FIRST found Ben". So, it should be "USA" in this precise case. Question: how exactly test code should look like?

CodePudding user response:

That would work:

const res = pm.response.json();

const first = res.people.find(p => p.nationality === 'USA');

pm.test('Check nationality', () => {
    pm.expect(first.name).eql('Ben');
    pm.environment.set('name', first.name);
})
  • Related