[SOLVED] Postman search for a specific value in JSON

Issue

This Content is from Stack Overflow. Question asked by Erhan Yumer

I’m trying to check response body if there’s a user which has last_name = “Holt” in following JSON file. with writing a test script in Postman

I’ve already tried:

pm.test("Last Name Holt", function () {
    var jsonData = JSON.parse(responseBody);
    value = pm.expect(jsonData[0].last_name).to.eql("Holt"); });

and

pm.test("Last Name Holt", function () {
    const responseJson = pm.response.json();
    pm.expect(responseJson.data[3].last_name).to.eql("Holt");
});

2nd code piece returns true but that’s directly looking into data[3] I want to search amongst all last_names in the file.



Solution

You can use the some() method to determine if at least one of the objects in data match your condition:

pm.test("Last Name Holt", function () {
  const responseJson = pm.response.json();
  pm.expect(responseJson.data.some(o => o.last_name === 'Holt')).to.be.true;
});


This Question was asked in StackOverflow by Erhan Yumer and Answered by Rory McCrossan It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?