Saving environment variables in an array

Hi

I get the following response from a GET request that I send:

[
  {
    "Id": 1,
    "Name": "Bank1",
    "site": "London",
    "Count": 27,
    "active": false
  },
  {
    "Id": 2,
    "Name": "Bank2",
    "site": "Manchester",
    "Count": 412,
    "active": false
  },
  {
    "Id": 3,
    "Name": "Bank3",
    "site": "Birmingham",
    "Count": 763,
    "active": true
  }
]

I am trying to save all of the ‘site’ responses to be used in my following request by creating the following test:

var jsonData = JSON.parse(responseBody);
pm.environment.set("site1", jsonData.site[0]);

However, my environment variable ‘site1’ is null when I was expecting London.

Can anyone advise me how I get the variable saved with the actual response for all 3 of the fields called site ?

Many thanks

With the current JSON response provided, the below snippet could work.

var jsonData = JSON.parse(responseBody);
console.log(jsonData[0].site);
pm.environment.set("site1", jsonData[0].site);

siteArray = [];
for (var i=0; i<jsonData.length; i++)
{
    var site = jsonData[i].site;
    //console.log(jsonData[i].site)
    siteArray.push(`${site}`);
}
console.log(siteArray);

image

2 Likes

Hey @carterusm69

To save each site value to an individual variable you could use something like this to loop through the objects of the response array. I’ve set them as global variables here but you can just which the scope to your needs.

_.each(pm.response.json(), (el, index) => pm.globals.set(`site${index + 1}`, el.site))

The index is there to give you a unique name but as it’s zero indexed, i’ve added a +1 to make it more human readable.

Or if you do want to store these are an array in a variable:

let sites = []
_.each(pm.response.json(), (el) => sites.push(el.site))

pm.globals.set("sites", JSON.stringify(sites))

To use them in a request, you would need to parse the array in the Pre-request Script to access each value:

let site = JSON.parse(pm.globals.get('sites'))

console.log(site[0])
console.log(site[1])
console.log(site[2])
4 Likes

@danny-dainton Hello!
In my first request I get list Instances

[
    {
        "id": "02f12923-888c-11eb-a4f4-a2fae9f0dab1"
    },
    {
        "id": "0d5acccd-b962-11eb-8bde-368c08a2a0c8"
    },
    {
        "id": "16f7a0d8-b9f7-11eb-8bde-368c08a2a0c8"
    }
]

Using your code i can store an array in variable

let ids = []

_.each(pm.response.json(), (el) => ids.push(el.id))

pm.environment.set("ids", JSON.stringify(ids))

Please tell me how to use each value from the array in the second request? What i have to write in pre request script?

Did anyone know how to use the array values in subsequent API request, inside pre-requisite script.

Hey @gmathai :wave:

Welcome to the Postman community! :postman:

Where in the request are you wanting to use those values?

Do you need the whole array, part of it, only a single item?

We would need to know more details to be able to help here. :pray:t2:

There are plenty of examples on here for looping through variables.

Have a search around the forum first.

Clue: array.shift()

It all depends on how those values are going to be used.

Using array.shift() in a script may not even be required, depending on the context :smiley:

I how no doubt some level of searching happened here, be it in the forum or on a search engine. You wouldn’t end up replying on a thread nearly a year later if that wasn’t the case. :joy:

1 Like