Assertion value for all arrays

Hi!
I´m trying to validate a value that appears in many results arrays. It is not possible to repeat the test every time, in this case I have 50 results, but this number is not a known value.
I have checked in help different solutions but without any success.
Could anyone help me with this? Thanks in advance!

I would need to check this value **jsondata.data[for each array].elasticScore
This is my current test:

var jsondata = JSON.parse(responseBody);

pm.test(“Elastic Score > 0 <= 1”, function () {
var jsondata = pm.response.json();
pm.expect(jsondata.data[0].elasticScore).to.be.greaterThan(0);
});

pm.test(“Elastic Score > 0 <= 1”, function () {
var jsondata = pm.response.json();
pm.expect(jsondata.data[1].elasticScore).to.be.greaterThan(0);
});

pm.test(“Elastic Score > 0 <= 1”, function () {
var jsondata = pm.response.json();
pm.expect(jsondata.data[2].elasticScore).to.be.greaterThan(0);
});

pm.test(“Elastic Score > 0 <= 1”, function () {
var jsondata = pm.response.json();
pm.expect(jsondata.data[3].elasticScore).to.be.greaterThan(0);
});

Parse your response directly to the data array.

You are also using the old method for parsing.

Use the following instead.

const array = pm.response.json().data

Once you have an array, you can use a forEach loop.

JavaScript Array forEach() Method (w3schools.com)

response.forEach(element => 
    pm.test(`Elastic Score ${element.elasticScore} > 0 <= 1`, () => {
        pm.expect(element.elasticScore).to.be.greaterThan(0);
    })
);

Use backticks and a string literal in the test case name so you can customise the name.

JavaScript Template Literals (w3schools.com)

On a side note, your test name, and your assertion don’t fully align. The assertion is “greater than zero”, but your test case name is “greater than zero and less than or equal 1”. If that is the actual test, then you need to update your assertion to match.

1 Like

Thanks for your help! It works perfect!!!

I have already changed the test name because I have copied from other one…