Extract and Compare Facet Response

Iā€™m running Postman from the web and I am trying to extract just the values from the response and then also setup a monitor that e-mails when the response values differ from the previous response. For example, the below is a response from an API body I crafted:

{
    "@search.facets": {
        "State": [
            {
                "count": 1,
                "value": "Michigan"
            },
            {
                "count": 1,
                "value": "California"
            }
        ]
    },
    "value": []
}

I am looking to have a clean response of just the values under the State array. From there, Iā€™d also like to have a monitor run every two hours (Iā€™m familiar how to setup the monitor) that will e-mail if there is any change from the return of:

Michigan
California

(at least in this example)

Thank You!

ā€œ@search.facetsā€ is using special characters so you will need to use bracket notation to target that element.

You can also use the Lodash _.map function to return just the values key for the State object.

const response = pm.response.json();

let states = _.map(response["@search.facets"].State, "value");

console.log(states);

Which returnsā€¦

image

You can also use the core JavaScript map function instead.

let states = response["@search.facets"].State.map(obj => obj.value);

I donā€™t use monitor so I canā€™t help you with that aspect.

Thank you!

I was able to figure out utilizing Monitor and SendGrid to generate an email every couple of hours.

The one other hiccup Iā€™m running into is writing an if/else statement where I want it to terminate at this step and not send an email is the array is the same. I set an environmental variable to be identical to the State environmental variable, however based on the console output I am writing this doesnā€™t work:

if (pm.environment.get(ā€œStateOrigā€) === ā€œStateā€) {
console.log(ā€œsameā€)
}
else {
console.log(ā€œdifferentā€)
}

It will write different everytime to the console even though the variable values are identical

They canā€™t be the same, so first step is to console log both and have a look in the console.

I suspect one is an array and the other is something else. A string perhaps.

You may need to JSON.parse() the array for the comparison to work.

First step is to look at those console logs and then share a screenshot.

They are identical right now as I set both variables the same. I wrote a function to compare the two by checking the length and going through the array.

Whatā€™s interesting is if I run it through the thick client, it works. However, in the cloud environment (where the scheduled monitor will run out of) it throws the following error:

Couldnā€™t evaluate the test script:
TypeError: Cannot read properties of undefined (reading ā€˜lengthā€™)

Hereā€™s the exact code:

function arraysEqual(arr1, arr2){
        if (arr1.length !== arr2.length) {
            return false;
        }

        for (let i = 0; i < arr1.length; i++){
            if (arr1[i] !== arr2[i]) {
                return false;
            }
        }
        return true;
}

if (arraysEqual(pm.environment.get("properties"),pm.environment.get("propertiesorig"))) {
    postman.setNextRequest(null);
    console.log("terminating");
}  else {
    pm.environment.set("propertiesorig",properties);
    console.log("continuing");
}

When working with arrays in Postman, the general advice is to JSON.stringify() the array when storing it, and JSON.parse() it when retrieving it. Otherwise it can sometimes cause issues with it being treated as a string.

However, your example is fairly simple and you should be able to get away without doing this.

In relation to your code, please read the following which goes into detail on how to compare arrays.

How to compare arrays in JavaScript? - Stack Overflow

Here are some example tests.

pm.collectionVariables.set("array1", ["Michigan", "California"]);
let array1 = pm.collectionVariables.get("array1");

pm.collectionVariables.set("array2", ["Michigan", "California"]);
let array2 = pm.collectionVariables.get("array2");

pm.collectionVariables.set("array3", ["California","Michigan"]);
let array3 = pm.collectionVariables.get("array3");

pm.test("array 1 matches array 2", function () { //pass
    pm.expect(array1).to.eql(array2);
});

pm.test("array 1 matches array 3", function () { // fail
    pm.expect(array1).to.eql(array3);
});

pm.test("array 1 matches array 2 - using Lodash", function () { // pass
    pm.expect(_.isEqual(array1, array2)).to.be.true;
});

pm.test("array 1 matches array 3 - using Lodash", function () { // fail
    pm.expect(_.isEqual(array1, array3)).to.be.true;
});

pm.test("array 1 matches array 3 - sorted using Lodash", function () { // pass
    pm.expect(_.isEqual(array1.sort(), array3.sort())).to.be.true;
});

Thank you again! I tied the environment variables to the collectionVariables since they would be dynamic and the Pass/Fail on the test works. Working on figuring out how to then tie that into if else statement:

if (pm.test("array 1 matches array 2", function () { //pass
    pm.expect(array1).to.eql(array2);
    })=="Pass") {
    console.log("same");
} else {
    console.log("different")
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.