API Testing 'A Beginners View': How to push list of values into a array and store as variable in POSTMAN using javascripting

In API testing sometimes we need to store a list of response values from one request, that needs to be used in another request without any usage of a data-driven approach. In such a situation, Javascript has an awesome method Array.push() Method.

Recently at work, I had a situation to store a list of values wherein got to know about the Array.push() method, so thought of sharing with all who are new to API testing like me :slight_smile:

The JavaScript Array.push() method adds a new element to the end of the array. When doing so, the array’s length property increases by one. After adding the new element to the end of the array, this method returns the new length of the array.

Example # Used in my daily work

var resp = JSON.parse(responseBody); //parsing the JSON response

console.log(resp); //printing the response in console

countArr = ; //array that would store the list of values

for (var i=0; i<resp.length; i++) //this will loop in through the data in response

{

//pushing the attribute value from response to the array

countArr.push(${resp[i].attributes.value});

console.log(countArr);

//storing the array as an environment variable for further use

pm.environment.set(‘countArr’, (countArr));

}

In Example, our array starts out with zero elements. The first console.log(resp) allows you to print the parsed JSON response in the console. We then loop in through the response and use the Array object’s push() method to add elements to the array ‘countArr’. Notice that when we do this, we wrap the call in a console.log() call, allowing us to see that the push() method has returned the list of records as expected. Then, we have used the pm.environment.set(“variable_key”, “variable_value”); to store the array as an variable for further use in the next requests.

7 Likes

My current Response is

“pinvalues”:slight_smile:
[
1,
3
]

i want to store this 2 value in global variable then i need to call this in another request like below.

Request

pinposition":
[
1,
3
]

how can i achieve it, i tried the above solution but still not able to achieve it. Any suggestion

Hey Jency, appears your post has somewhat been corrupted regarding your notes on this item. Can you elaborate again as to what you used for the “countArr =; //array that would store the list of values”

Would be very helpful if you could tell me what you had entered after the equal sign as it was replace with an image of a square for some reason. Thank you!!!

Hi @turtlkky

Thanks for reaching out, its not corrupted its a empty array definition editor is projecting it as square when I put it :slight_smile:

Its as below:
image

Hope now its clear :slight_smile:

Hi @altimetry-geoscient6 thanks for connecting :slight_smile:

Can you pls share your error msg here to understand better, so that I can help you correctly?

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