Home > Software engineering >  Postman search for a specific value in JSON
Postman search for a specific value in JSON

Time:09-19

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.

CodePudding user response:

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