'Postman: filter nested response

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?



Solution 1:[1]

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);
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lucas-nguyen-17