# How to loop over array to change input of request?

**URL:** <https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [November 16, 2020, 3:06pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733 "2020-11-16T15:06:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 16, 2020, 3:06pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/1 "2020-11-16T15:06:04Z")

</div>

Hello Community,

I am quite new to Postman and I am trying to solve the following problem:

I am sending requests, that depend on a longitude and a magnitude (2 keys with the belonging values).

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/a/ca5115cf3d759b29eed3843c647be9b36c3683f3.png)  
I want to make the values variable such that I can read them from two arrays with longitudes and latitudes and iterate over these arrays until all requests for all information of the wanted different locations are sent.

I am not quite sure if and how it is possible. I only found posts regarding running tests in loops.  
Maybe someone can help.

Thank you very much in advance.

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [November 17, 2020, 5:04am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/2 "2020-11-17T05:04:07Z")

</div>

@mjmag Welcome to the Community 👋

So here you already have an array with list of values for Latitude and Longitude and you need to read from it? Or those values are getting generated from different requests or you are generating the values for them?

Or you are looking to save the Longitude and Latitude values in an array to use them in the further requests? Can you please details it here.

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 17, 2020, 10:28am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/3 "2020-11-17T10:28:30Z")

</div>

Thank you very much for that fast answer.  
Sorry for not specifying correctly.

I already have two arrays with list of values for Latidudes and the belonging Longitudes, like:

Lats = [1,2,3,4,5]  
Longs = [1,2,3,4,5]

At every request I need to fill in one Latitude and one Longitude. Now I want to start the first request with the first value pair and loop over the arrays for further requests and save the information (as seperate JSON files) for each of these requests.

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [November 17, 2020, 10:55am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/4 "2020-11-17T10:55:02Z")

</div>

@mjmag Can you try pasting the below in your pre-request script,

```auto
pm.environment.set("Latitude", value[Lats]);
pm.environment.set("CountofLats", Number(Lats) + 1);

pm.environment.set("Longitude", value[Longs]);
pm.environment.set("CountofLongs", Number(Longs) + 1);

```

and try to access the variables Latitude and Latitude in your request.

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 17, 2020, 11:23am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/5 "2020-11-17T11:23:03Z")

</div>

This looks exactly like what I am looking for… But I get this Error:

 ![grafik](https://us1.discourse-cdn.com/getpostman/original/3X/7/6/76ac21c3bd67cd8d75b35ad166e37a7354f1ce44.png)

is it correct that I am defining the arrays also in the Pre-request Script?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [November 17, 2020, 11:31am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/6 "2020-11-17T11:31:34Z")

</div>

> **[JavaScript Arrays](https://www.w3schools.com/js/js_arrays.asp)**

This helps you on the arrays. So it should look like:

```auto
var Lats = ["1","2","3","4"];

```

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 17, 2020, 12:12pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/7 "2020-11-17T12:12:03Z")

</div>

Oops, sorry for that, I was writing parallel with a coding language that is syntactically “simpler”…

But still with the correct syntax I get the same ReferenceError:

 ![grafik](https://us1.discourse-cdn.com/getpostman/original/3X/c/1/c16ea300c1f63aff5b7e38b5a0a8500efc140922.png)

Sorry to be so demanding. I am just lacking of experience and therefore also of creativity to solve it…

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 17, 2020, 12:43pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/8 "2020-11-17T12:43:20Z")

</div>

No worries @mjmag, we all start somewhere!

The issue with your code is that you are trying to reference a `value` object that does not exist.

I would approach this slightly different because even if this code worked to get you lat/long values, it wouldn’t loop, it would only run one time.

So let’s start by assuming you have your latitude and longitude arrays stored in environment variables: `latitudeArray` and `longitudeArray`.

In your prerequest script, you will want to add something like this:

```auto
// Load the latitude and longitude arrays from the environment
const latitudeArray = pm.environment.get('latitudeArray');
const longitudeArray = pm.environment.get('longitudeArray');

// Load the index counter
let index = pm.environment.get('latLongIndex');
if(!index){
  index = 0;
}

// Set the variables for this iteration and increment the counter
pm.environment.set('latitude', latitudeArray[index]);
pm.environment.set('longitude', longitudeArray[index]);
pm.environment.set('latLongIndex', (index+1));

```

Then in your test scripts, you would add the following code:

```auto
const latitudeArray = pm.environment.get('latitudeArray');
const longitudeArray = pm.environment.get('longitudeArray');
const index = pm.environment.get('latlongIndex');

// Check to see if we need to do another loop
if(index < latitudeArray.length && index < longitudeArray.length) {
  // If another loop is required, set the next request to this one. 
  postman.setNextRequest(request.name);
}
else {
  // If another loop is not required, clear out the index variable for next time.
  pm.environment.unset('latLongIndex');
}

```

Note - you cannot run multiple requests manually. You would have to run this through the collection runner for it to loop through all of your lat/long values.

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 18, 2020, 7:41am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/9 "2020-11-18T07:41:28Z")

</div>

This already helps a lot - thank you very much!

I think we are getting close, but something seems to go wrong with setting the index.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/9/49e8de4cbf13881a6007d850260e276bc8ae1327.png)

Somehow it tries to read it instead of setting it to zero.

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 18, 2020, 11:41am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/10 "2020-11-18T11:41:02Z")

</div>

It seems to be a type problem.

I modified the code a bit with an already existing variable called ‘latLongIndex’ in the environment:

```
// Load the latitude and longitude arrays from the environment

const latitudeArray = pm.environment.get('latitudeArray');

const longitudeArray = pm.environment.get('longitudeArray');

// Load the current index 

const index = Number(pm.environment.get('latLongIndex'));

console.log("The value of the index is "+index)

// Set the variables for this iteration and increment the counter

pm.environment.set('latitude', latitudeArray[index]);

pm.environment.set('longitude', longitudeArray[index]);

pm.environment.set('latLongIndex', (index+1));

console.log(pm.environment.get('latLongIndex'));

```

Then I get this errror:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/1/314100adf4f5733854bfdcf510cdbca2ae1d2d28.png)

I don’t really understand why it is not recognizing an integer as such.

Here a snapshot of my variables for the environment when I start the collection run:

![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/3/535718e095b6bff3f9bebbccf3ab6561b1de2fed.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 18, 2020, 12:46pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/11 "2020-11-18T12:46:48Z")

</div>

Everything stored in a variable is a `string` so you would need to convert that `string` to an `integer`.

For example: `parseInt(pm.environment.get('var_name'))`

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 18, 2020, 1:02pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/12 "2020-11-18T13:02:03Z")

</div>

Is there any way to parse the Arrays as well?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 18, 2020, 2:50pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/13 "2020-11-18T14:50:05Z")

</div>

You can use `JSON.parse(myVariable)` to parse an array.

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 19, 2020, 11:33am UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/15 "2020-11-19T11:33:51Z")

</div>

Guys, thanks a lot… you got me to a point where I have a script, that is doing what I want it to do.

Now I just have one question left, which is less technical (hopefully):  
Every request is responding with a JSON file with the wanted information. How can I save these responses during the loop … or… where can I find it?  
After the successful run of the Runner I can choose -\> _Export Results_. But this is giving me a report of the run (i.e. running time etc.).

How can I find the actual responses of the API?

If someone is interested here the code that works:

Pre-request:

```
// Load the latitude and longitude arrays from the environment

const latitudeArray = JSON.parse(pm.environment.get('latitudeArray')); // Parse from string to array

const longitudeArray = JSON.parse(pm.environment.get('longitudeArray'));

// Check array by log.output

console.log(latitudeArray)

// Load the current index 

const index = parseInt(pm.environment.get('latLongIndex')); //Parse from string to Integer

// Some log.outputs to follow what's happening

console.log("The value of the index is " + index);

console.log((index+1)+". entry of array: "+ parseFloat(latitudeArray[index]));

// Set the variables for this iteration and increment the counter

pm.environment.set('latitude', parseFloat(latitudeArray[index]));

pm.environment.set('longitude', parseFloat(longitudeArray[index]));

pm.environment.set('latLongIndex', (index+1));

```

Test:

```
const latitudeArray = JSON.parse(pm.environment.get('latitudeArray'));

const longitudeArray = JSON.parse(pm.environment.get('longitudeArray'));

const index = parseInt(pm.environment.get('latLongIndex'));

// Check to see if we need to do another loop

if(index < latitudeArray.length && index < longitudeArray.length) {

  // If another loop is required, set the next request to this one. 

  postman.setNextRequest("RequestName");

}

else {

  // If another loop is not required, set index back to 0 for next time.

pm.environment.set('latLongIndex', 0);

}
```

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 19, 2020, 12:17pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/16 "2020-11-19T12:17:46Z")

</div>

What do you want to do with the responses?

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 19, 2020, 12:25pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/17 "2020-11-19T12:25:28Z")

</div>

I want to export them in a JSON format to visualize them in a different program.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 19, 2020, 12:30pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/18 "2020-11-19T12:30:43Z")

</div>

I’ve never tried to do this myself, so I’m not positive if there is a way to do it natively in the app.

Something you can always do is save the responses to an environment variable, then when the collection run is done just copy the value out of postman and use it wherever you need. Something like this:

```auto
let responses = pm.environment.get('responses');
if(!responses){
  responses = [];
}
else {
  responses = JSON.stringify(responses);
}

const jsonData = pm.response.json();
responses.push(jsonData);

pm.environment.set('responses', JSON.stringify(responses));
```

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 19, 2020, 1:19pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/19 "2020-11-19T13:19:52Z")

</div>

Jep, seems like I have to use “Newman” for this.

My first solution is just saving the JSONs as global variables, but they have to be downloaded and deleted after each run:

```
// Set global variable with response body

pm.globals.set('response_body'+index, JSON.stringify(pm.response.json()))

```

Thanks again for the fast and helpful replies!

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 19, 2020, 1:33pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/20 "2020-11-19T13:33:56Z")

</div>

How about you save all the responses to a variable, then you add a request at the very end of your collection that saves the response data to Google Sheets?

That way you have all the results in the cloud, it’s saved automatically, and it’s easily maintainable?

> **[Method: spreadsheets.values.append  |  Google Sheets  | ...](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append)**

---

<div class="post-metadata">

**Author:** ![mjmag](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mjmag](https://community.postman.com/u/mjmag)\
**Post date:** [November 20, 2020, 4:56pm UTC](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733/21 "2020-11-20T16:56:26Z")

</div>

That’s quite a nice idea for a first solution.  
I will look at it a little more deeply on Monday and try to implement it.  
Thanks a lot and I will keep you informed how it works.

[Next page](https://community.postman.com/t/how-to-loop-over-array-to-change-input-of-request/17733.md?page=2)
