Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Postman search for a specific value in JSON

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading