For loop not working in a collection run against a JSON data file

This is my test data (saved as numbers.json ):

[
  {
    "numbers": 1422.39
  },
  {
    "numbers": 429.01
  }
]

I ran this test in the Postman Runner.

My code is:

const jsonData = pm.response.json();
for (let i=0; i < jsonData.length; i++)
{
    console.log("Response " + [i] + " data = " + data["numbers"]);
}

The output looks like this:

Output results

And if I use the code:

console.log("Response " + [i] + " data = " + data[i].numbers);

Then I will get this error:

TypeError: Cannot read property 'numbers' of undefined

My responseBody is (same as the data file - it is a copy, to test it works):

[
  {
    "numbers": 1422.39
  },
  {
    "numbers": 429.01
  }
]

Now I know that when I run my other tests, and using data.numbers[0].line1 it works.

i.e.

[
  "numbers":
  {
    "line1": 1357
  },
  {
    "line1": 2468
  }

Hi @kev-ross2,

I am not sure where your code is loading the variable called data from, as this variable isn’t defined in your snippet. (It’s obviously loading something from somewhere, hence it’s still managing to display values in some scenarios)

If you’re trying to read the numbers from your response json, then I think you want to change your log statement to say:

console.log("Response " + [i] + " data = " + jsonData[i]["numbers"]);

Thank you for your response @neilstudd,

data is used for when I run the collection runner and I call my numbers.json file (as referenced at the top of my question), which I have stored on my computer.

My actual:

for (let i = 0; i <= jsonData.length; i++)
{
    // 1st tests
    pm.test("expected line1 = " + (data["line1"]) + " | Actual line1 = " + (jsonData[i].line1), function () {
        pm.expect(jsonData[i].line1).to.equal(data["line1"]);
    });

This then returns:

iteration 1 = expected line1 = 1357 | Actual line1 = 1357
iteration 1 = expected line1 = 1357 | Actual line1 = 2468
iteration 2 = expected line1 = 2468 | Actual line1 = 1357
iteration 2 = expected line1 = 2468 | Actual line1 = 2468

So i am just really confused.