# Collection variable how to set from tests

**URL:** <https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968>\
**Category:** Help Hub\
**Created:** [September 8, 2022, 4:43pm UTC](https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968 "2022-09-08T16:43:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pamirian123](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@pamirian123](https://community.postman.com/u/pamirian123)\
**Post date:** [September 8, 2022, 4:43pm UTC](https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968/1 "2022-09-08T16:43:35Z")

</div>

hello,  
I’m new to this and trying to do a flow where 1 send request will create a variable called orgID from the response it gets and pass it on to the next send request.

the first flow when I run it, generates this below.  
[{ “id”: “11223344”, “name”: “somename”, “url”: “[https://someurl](https://someurl)”, “api”: { “enabled”: true }, “licensing”: { “model”: “co-term” }, “cloud”: { “region”: { “name”: “North America” } } } ]

I want to create a collection variable called myOrg using the tests section and have myOrg have the value 11223344.

this is the tests I have so far but it’s just dumping the entire response back on console starting from id which isn’t what I need, I need myOrg to contain only the value 11223344.

var jsonData = JSON.parse(responseBody);  
pm.collectionVariables.set(“myOrg”, pm.response.json.id);  
console.log(pm.response.json(“myOrg”));

thanks for any help.

---

<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:** [September 9, 2022, 11:30am UTC](https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968/2 "2022-09-09T11:30:39Z")

</div>

```auto
pm.collectionVariables.set(“myOrg”, pm.response.json().id);
console.log(pm.CollectionVariables.get("myOrg"));

```

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

---

<div class="post-metadata">

**Author:** ![pamirian123](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@pamirian123](https://community.postman.com/u/pamirian123)\
**Post date:** [September 9, 2022, 11:59am UTC](https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968/3 "2022-09-09T11:59:27Z")

</div>

the response I get back is “undefined”.

---

<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:** [September 9, 2022, 3:11pm UTC](https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968/4 "2022-09-09T15:11:56Z")

</div>

On closer inspection, it looks like you have an array.

```auto
const response = 

[{ "id": "11223344", "name": "somename", "url": "https://someurl", "api": { "enabled": true }, "licensing": { "model": "co-term" }, "cloud": { "region": { "name": "North America" } } }]

console.log(response);

pm.test('response is an array', () => {
    pm.expect(response).to.be.an('array');
});

console.log(response[0].id)

```

Therefore the following should work.

```auto
const response = pm.response.json()
pm.collectionVariables.set("myOrg", response[0].id);
console.log(pm.CollectionVariables.get("myOrg"));

```

---

<div class="post-metadata">

**Author:** ![pamirian123](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@pamirian123](https://community.postman.com/u/pamirian123)\
**Post date:** [September 9, 2022, 3:36pm UTC](https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968/5 "2022-09-09T15:36:30Z")

</div>

fantastic,  
it gives me “11223344”, how would I remove the " please and get only 11223344?

---

<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:** [September 9, 2022, 4:21pm UTC](https://community.postman.com/t/collection-variable-how-to-set-from-tests/38968/6 "2022-09-09T16:21:34Z")

</div>

Postman uses JavaScript under the hood.

```auto
const response = pm.response.json()
pm.collectionVariables.set("myOrg", parseInt(response[0].id));
console.log(pm.CollectionVariables.get("myOrg"));

```

> **[How to Convert String to Number in JavaScript](https://www.w3docs.com/snippets/javascript/how-to-convert-string-to-number-in-javascript.html)**
>
> There are several methods of converting a string into a number: parseInt() #; parseFloat() #, Number() #, etc. See examples.
