# Getting a specific variable from a list of variables

**URL:** <https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917>\
**Category:** Help Hub\
**Tags:** variables, collections, extract\
**Created:** [March 17, 2024, 11:29am UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917 "2024-03-17T11:29:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfrenn](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pfrenn](https://community.postman.com/u/pfrenn)\
**Post date:** [March 17, 2024, 11:29am UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917/1 "2024-03-17T11:29:35Z")

</div>

Basics:  
I have never coded in postman and I may not refer to the correct nomenclature or naming. If so, I am sorry, but I am happy to learn 🙂

Scope:  
I am calling a server to get various values from the server.  
it may be login tokens, IDs, carts etc.  
I also have several postman environments with a copy of each environment but with a change in the server domain name only. I would like to understand how I can simplify this mess of environments.

Issue:  
This is an example of a script I have an issue with. This is applicable to other server response calls also.

Question:  
1.How can I now pick the token from the response from the server? OR any of the other varibles?  
2. how should I get this to work? Please share example code as I have read through some of the postman areas, but I do not have enough coding knowledge to understand it.

Code:  
This is a a call to get a login token to an ecommerce site

URL: `{{base_url}}/rest/V1/integration/customer/token?username=Paste_username&password=Paste_password`

Test environment tab:

```auto
pm.collectionVariables.set("customer_token","token");

```

Response in postman:

```auto
[
    {
        "token": "<token in the response>",
        "status": 200,
        "success": true,
        "message": "allowed"
    }
]

```

---

<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:** [March 17, 2024, 11:45am UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917/2 "2024-03-17T11:45:08Z")

</div>

Hey @pfrenn 👋

Welcome to the Postman Community! :postman:

You would first need to get all the response using:

```auto
const resp = pm.response.json();

```

From here you will be able to get specific parts of the data that you have saved to the local variable.

As your response is an array and you want to access the first object in the array, you need to use `[0]` if you didn’t use that, the stored value would be `undefined`.

```auto
const resp = pm.response.json();
pm.collectionVariables.set("customer_token", resp[0].token);

```

Will this stored as a Collection variable, you can use this in the URL by using the `{{customer_token}}` variable name.

* * *

This are all quite basic workflows in Postman and I would highly recommend working through the training in these Collections, to get more familiar with these fundamental techniques.

[https://www.postman.com/postman/workspace/postman-galaxy-training](https://www.postman.com/postman/workspace/postman-galaxy-training)

---

<div class="post-metadata">

**Author:** ![pfrenn](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pfrenn](https://community.postman.com/u/pfrenn)\
**Post date:** [March 17, 2024, 11:56am UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917/3 "2024-03-17T11:56:07Z")

</div>

Thank you for swift reply.  
Keen to see if it worked I pasted your code into the call and the token is not allocated to the “customer\_token” value.

A bit puzzled since the customer\_token is in the environment.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/0/a082e00b367a5a28d45ce0fffe5c86409b9d3161.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/0/30b9c184bc0645d64fdff514916d3e8e53bb8aa5.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:** [March 17, 2024, 12:03pm UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917/4 "2024-03-17T12:03:05Z")

</div>

That was setting a _Collection_ level variable in the script, I know that’s not the Collection Variables section as you cannot change the type of those.

If you open your Collection in a new tab, there is a Variables sections within that.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/9/29358df62601553b0922d6e131b355147862a08f.png)

* * *

This is all covered in those training course, to avoid answering a series of similar questions, I would highly suggest you work through those courses or at the very least, take a look at our documentation.

> **[Postman documentation overview | Postman Learning Center](https://learning.postman.com/docs/introduction/overview/)**
>
> Postman documentation overview: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

---

<div class="post-metadata">

**Author:** ![pfrenn](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pfrenn](https://community.postman.com/u/pfrenn)\
**Post date:** [March 17, 2024, 12:19pm UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917/5 "2024-03-17T12:19:00Z")

</div>

> [@danny-dainton](#):
>
> answering

Thank you.

I am using the postman app in windows and it does not have that tab.

I will look into those courses.

---

<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:** [March 17, 2024, 12:24pm UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917/6 "2024-03-17T12:24:19Z")

</div>

Every OS version has that tab. 🤔

In the right sidebar, click on your Collection. That will open the Collection in a tab like it does when you open a request from the Collection.

Alternatively, in the script change `collectionVariables` for `environment` and you would see it listed in the view you have.

---

<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:** [April 16, 2024, 12:24pm UTC](https://community.postman.com/t/getting-a-specific-variable-from-a-list-of-variables/58917/7 "2024-04-16T12:24:57Z")

</div>

Closed on April 16, 2024, 12:24pm UTC

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