# Use a list environment variable within pre-request scriipt

**URL:** <https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093>\
**Category:** Help Hub\
**Tags:** pre-request-script\
**Created:** [August 5, 2024, 3:24pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093 "2024-08-05T15:24:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![daiwattnokia](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daiwattnokia](https://community.postman.com/u/daiwattnokia)\
**Post date:** [August 5, 2024, 3:24pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/1 "2024-08-05T15:24:24Z")

</div>

Would anyone know how to import a list (of IPv4 addresses in this instance) into a postman Pre-request script? So for example - I have a request that is working, but uses a ‘hard-coded’ list of IPs in the Pre-request script - what I’m after idsa way to import this list somehow, maybe an environment variable or an imported list:

```auto
var deployments = [];
// Iterate over the range for i (1-4) number of nodes
var nodeIP= ["10.0.1.1","10.0.1.2","10.0.1.3","10.0.2.1","10.0.2.2"]
for (var x = 0; x < nodeIP.length; x++) {
    // Iterate over the range for i (1-30) for the number of deployment
                ;
        // Create deployment object
        var deployment = {
            "template-name": "SRL_ACL_System_Filter",
            "target-data": JSON.stringify(pstmtsrlpayload),
            "targets": [
                {
                    "target": "/nsp-equipment:network/network-element[ne-id='"+nodeIP[x]+"']",
                    "target-identifier-value": "system-filter"
                }
            ],
            "deployment-action": "deploy",
            "merge": false
        };

```

I have tried things like the following without success:

`var nodeIP = pm.environment.get("pdiSrlList1");`

where the environment variable is:

```auto
pdiSrlList1 = "10.0.1.1","10.0.1.2","10.0.1.3","10.0.2.1","10.0.2.2"

```

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [August 6, 2024, 12:36pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/2 "2024-08-06T12:36:08Z")

</div>

HI @daiwattnokia. Welcome to the Postman Community :postman_logo:

What will be the data source of the list of IPs? Are you looking to get them from an API, have them in a csv or JSON file, etc? It’ll be helpful to know how this data will be presented to suggest a good way of programmatically “importing” it.

---

<div class="post-metadata">

**Author:** ![daiwattnokia](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daiwattnokia](https://community.postman.com/u/daiwattnokia)\
**Post date:** [August 6, 2024, 1:08pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/3 "2024-08-06T13:08:54Z")

</div>

Thanks @gbadebo-bello,  
I don’t mind how they are presentecd - CSV, JSON, postman environment variable or something else - that is easy to adapt. So let’s say CSV - that’s the simplest - I have to present this as an easy method to a field community, who won’t want any excessive complexity

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [August 6, 2024, 2:02pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/4 "2024-08-06T14:02:19Z")

</div>

Hi @daiwattnokia.

What errors did you get when you used an environment variable? Can you share maybe a sample code of how you’re using this with an environment variable. Ideally, that should work for your usecase.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 6, 2024, 3:07pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/5 "2024-08-06T15:07:00Z")

</div>

You need to store the variable as an array, and then JSON parse it otherwise it will be treated as a string.

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

```auto
var nodeIP =JSON.parse(pm.environment.get("nodeIP"));

for (var x = 0; x < nodeIP.length; x++) {
    console.log(nodeIP[x]);
}

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/8/c8120a95668e0a892e87202dc1356cbe44b6a38e.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 6, 2024, 3:20pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/6 "2024-08-06T15:20:06Z")

</div>

If you want to use the collection runner and JSON as the input file, then the JSON file should look like the following.

```auto
[
    {
        "nodeIP": [
            "10.0.1.1",
            "10.0.1.2",
            "10.0.1.3",
            "10.0.2.1",
            "10.0.2.2"
        ]
    }
]

```

You don’t need to parse the JSON using this method.

```auto
var nodeIP = pm.iterationData.get("nodeIP");

for (var x = 0; x < nodeIP.length; x++) {
    console.log(nodeIP[x]);
}

```

---

<div class="post-metadata">

**Author:** ![daiwattnokia](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daiwattnokia](https://community.postman.com/u/daiwattnokia)\
**Post date:** [August 7, 2024, 10:45am UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/7 "2024-08-07T10:45:22Z")

</div>

Thanks will try those suggestion today and get back to you

---

<div class="post-metadata">

**Author:** ![daiwattnokia](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daiwattnokia](https://community.postman.com/u/daiwattnokia)\
**Post date:** [August 7, 2024, 4:35pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/8 "2024-08-07T16:35:19Z")

</div>

Thanks Mike, that worked perfectly 😀

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 7, 2024, 4:46pm UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/9 "2024-08-07T16:46:00Z")

</div>

Did you really mark your own post as the solution? Mmmm.

---

<div class="post-metadata">

**Author:** ![daiwattnokia](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@daiwattnokia](https://community.postman.com/u/daiwattnokia)\
**Post date:** [August 8, 2024, 8:40am UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/10 "2024-08-08T08:40:21Z")

</div>

Sorry, my mistake, I’ll correct that

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 11, 2024, 8:40am UTC](https://community.postman.com/t/use-a-list-environment-variable-within-pre-request-scriipt/65093/11 "2024-08-11T08:40:25Z")

</div>

Closed on August 11, 2024, 8:40am UTC

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