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

**URL:** <https://community.postman.com/t/for-loop-not-working-in-a-collection-run-against-a-json-data-file/25010>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [June 18, 2021, 12:09pm UTC](https://community.postman.com/t/for-loop-not-working-in-a-collection-run-against-a-json-data-file/25010 "2021-06-18T12:09:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kev-ross2](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@kev-ross2](https://community.postman.com/u/kev-ross2)\
**Post date:** [June 18, 2021, 12:09pm UTC](https://community.postman.com/t/for-loop-not-working-in-a-collection-run-against-a-json-data-file/25010/1 "2021-06-18T12:09:02Z")

</div>

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

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

```

> I ran this test in the Postman Runner.

My code is:

```javascript
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](https://i.stack.imgur.com/ZYSaY.png)](https://i.stack.imgur.com/ZYSaY.png)

And if I use the code:

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

```

Then I will get this error:

```auto
TypeError: Cannot read property 'numbers' of undefined

```

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

```json
[
  {
    "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.

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

```

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [June 18, 2021, 12:58pm UTC](https://community.postman.com/t/for-loop-not-working-in-a-collection-run-against-a-json-data-file/25010/2 "2021-06-18T12:58:02Z")

</div>

Hi @kev-ross2,

> [@kev-ross2](#):
>
> ```auto
> const jsonData = pm.response.json();
> for (let i=0; i < jsonData.length; i++)
> {
> console.log("Response " + [i] + " data = " + data["numbers"]);
> }
> 
> ```

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"]);

```

---

<div class="post-metadata">

**Author:** ![kev-ross2](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@kev-ross2](https://community.postman.com/u/kev-ross2)\
**Post date:** [June 18, 2021, 2:16pm UTC](https://community.postman.com/t/for-loop-not-working-in-a-collection-run-against-a-json-data-file/25010/3 "2021-06-18T14:16:24Z")

</div>

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:

```javascript
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:

```nohighlight
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.
