To assert that the array in the response body contains a substring string value in all its elements


New to Postman and finding this assertion very difficult to code.
In the example below, I have an array Customer with 3 elements.
I want to check if the firstName or LastName of each element starts with “JO”, “ADA” or “ZA”. I’ve tried a for loop with if else statements inside, but it’s the mixture of substring of 2 and 3 characters that’s making it very difficult I think.

I don’t expect anyone to do it for me, just to show me the right direction I guess. Thanks for any help.


Hi @scottadamspm,

Rather than worrying about substring length, you can use .startsWith on a Javascript string to check that it begins with that value (regardless of that value’s length).

Personally, here’s how I’d do it (there are probably some additional efficiencies that can be made):

pm.test("All names begin with JO, ADA or ZA", function () {
    var jsonData = pm.response.json();
    allNamesValid = true;

    _.each(jsonData.Customer, (customer) => {
        if (!doAnyNamesMatch(customer.firstName) && !doAnyNamesMatch(customer.lastName)) {
            console.log(`${customer.firstName} ${customer.lastName} does not match pattern`)
            allNamesValid = false;


function doAnyNamesMatch(field) {
    fieldValue = field.toUpperCase();
    return fieldValue.startsWith("BO") || fieldValue.startsWith("ADA") || fieldValue.startsWith("ZA")

A few notes about my approach:

  • _.each is an alternate method for iterating over items in a list by using Lodash syntax - you can see some more simple usage examples in this article. If you’re using some other for loop that you understand better, it’s probably just as good for your purpose :slight_smile:
  • I set a variable allNamesValid at the start of my test, and assume it to be true, and then I just update the value to false if I encounter a value which does not match.
  • Because the startsWith check is a little lengthy (and we want to run the check against both firstName and lastName), I moved this into a separate function called doAnyNamesMatch, and the loop checks both fields to see if they start with any of those three values.

The result is a nice green mark in your tests if all the names are okay:


…and if it comes across a name which does not match the pattern, the test will fail and output the offending names to the console:


1 Like
pm.response.json().Customer.filter((a) =>
    ["JO", "ADA", "ZA"].some((item) => a.firstName.toLowerCase().includes(item.toLowerCase()) || a.firstName.toLowerCase().includes(item))

i think this is easier to use:

  1. Here, filter option returns element that matches the given condition

  2. And the condition is that atleast one or more items in the array [“JO”, “ADA”, “ZA”] should be included in either firstname or lastname , you can change the includes with startwith as @neilstudd mentioned if you wnat to check the name starts with it

read about array prototype:

1 Like

Thank you very much @neilstudd. That works perfectly. I put it into a for loop as I can understand that better for now but never thought of the .startWith function. Really appreciate your help

Thanks @praveendvd for your suggestion also. There are many ways to skin a cat :slight_smile:

All the best