# Collection Variable not working

**URL:** <https://community.postman.com/t/collection-variable-not-working/27728>\
**Category:** Help Hub\
**Tags:** variables, collections, json\
**Created:** [September 10, 2021, 10:57pm UTC](https://community.postman.com/t/collection-variable-not-working/27728 "2021-09-10T22:57:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesrollinsjr](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jamesrollinsjr](https://community.postman.com/u/jamesrollinsjr)\
**Post date:** [September 10, 2021, 10:57pm UTC](https://community.postman.com/t/collection-variable-not-working/27728/1 "2021-09-10T22:57:16Z")

</div>

**Your question may already have an answer on the community forum. Please search for related topics, and then read through the [guidelines](https://community.postman.com/t/about-the-just-getting-started-category/37) before creating a new topic.**

> Here’s an outline with best practices for making your inquiry.

I’m trying to store a value from the response as a collection variable.

 ![Capturepost](https://us1.discourse-cdn.com/getpostman/original/2X/a/af54e15299ece3e56b92be80dec7c2cb42629e3b.png)

I use the command, pm.collectionVariables.set(“details\_id”, responseJson.Details.id); in a test function.

I’ve tried two different requested to try and get the information. One is a GET request which gives the error Cannot read property ‘id’ of undefined. The other is a PUT request which doesn’t give an error but when I look at the collection variables the current variable field is empty.

 ![Capturevalue](https://us1.discourse-cdn.com/getpostman/original/2X/1/19e9ef9c42cb4da42e8a4fe26a70a7a4070610f5.png)

---

<div class="post-metadata">

**Author:** ![Chris](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chris/32/6637_2.png) [@Chris](https://community.postman.com/u/Chris)\
**Post date:** [September 10, 2021, 11:21pm UTC](https://community.postman.com/t/collection-variable-not-working/27728/2 "2021-09-10T23:21:01Z")

</div>

Hey @jamesrollinsjr - welcome to the community!

I might be missing some context here but is `responseJson` the response you’ve saved before passing in the `id` key as a variable?

I tried to reproduce the issue but was able to store a value from the request response as a collection level variable. Here’s the snippet I used and a link to the public workspace/collection that you’re more than welcome to fork.

```auto
// Log response value in console - args.id is specific to the response I'm working with here.
console.log(pm.response.json().args.id);

// Set response value as a colleciton variable
pm.collectionVariables.set("details_id", pm.response.json().args.id)

```

The mocked up response here:

```json
{
    "args": {
        "id": "2f9b2d80-8e11"
    },
    "headers": {
        "x-forwarded-proto": "https",
        "x-forwarded-port": "443",
        "host": "postman-echo.com",
        "x-amzn-trace-id": "Root=1-613be75f-3c314d1d7d948138406c0d19",
        "user-agent": "PostmanRuntime/7.28.4",
        "accept": "*/*",
        "postman-token": "9c98346c-6102-49bf-9b7e-703300400a2a",
        "accept-encoding": "gzip, deflate, br",
        "cookie": "sails.sid=s%3ArIXjmz1CY1sQJjmr7Ag1KFJPbtVTaLjI.NAeEjIgLwMoi7pMm8w1ByyoRSXKKSJc6bCf5qWd%2FOYI"
    },
    "url": "https://postman-echo.com/get?id=2f9b2d80-8e11"
}

```

**Public Workspace**

- [Postman](https://www.postman.com/product-support-enterprise-lab/workspace/Public-Workspace~7c680210-0825-4c1e-9ded-80b7bb811d7c/collection/16283721-3a344938-9d18-44d8-8cfc-e246bb3282c6)

---

<div class="post-metadata">

**Author:** ![jamesrollinsjr](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jamesrollinsjr](https://community.postman.com/u/jamesrollinsjr)\
**Post date:** [September 11, 2021, 1:57am UTC](https://community.postman.com/t/collection-variable-not-working/27728/3 "2021-09-11T01:57:02Z")

</div>

> [@Chris](#):
>
> `pm.response.json()`

Yes, the full script is as such:

pm.test(“Get Message Number”, () =\> {

```
const responseJson = pm.response.json();

pm.response.to.have.status(200);

pm.collectionVariables.set("details_id", pm.response.json().Details.id);

```

});

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [September 11, 2021, 6:39am UTC](https://community.postman.com/t/collection-variable-not-working/27728/4 "2021-09-11T06:39:19Z")

</div>

Hi @jamesrollinsjr 👋

In the response “Details” is an array, so it may work with Details[0].

```auto
pm.collectionVariables.set("details_id", pm.response.json().Details[0].id);

```

Parsing the JSON response is one of the common challenges we face. Welcome to the club 😊 But this will get better once you are used to JSON responses.

I suggest you to use [https://jsonpathfinder.com/](https://jsonpathfinder.com/), this helped me a lot during my initial testing days. Also I wrote a small blog [here](https://bpricilla.medium.com/how-to-easily-locate-your-json-object-in-your-api-response-647549ee8b2) about it, feel free to read and provide your feedback! All the best 🥳

---

<div class="post-metadata">

**Author:** ![jamesrollinsjr](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jamesrollinsjr](https://community.postman.com/u/jamesrollinsjr)\
**Post date:** [September 11, 2021, 7:38am UTC](https://community.postman.com/t/collection-variable-not-working/27728/5 "2021-09-11T07:38:52Z")

</div>

I originally tried that with the GET request and it didn’t work so I didn’t think to try it in the PUT request which does work. Any ideas why the GET request acts differently?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [September 11, 2021, 12:53pm UTC](https://community.postman.com/t/collection-variable-not-working/27728/6 "2021-09-11T12:53:17Z")

</div>

@jamesrollinsjr Yes for GET request, the response structure might be different. so the same snippet might not work. And I believe as per the documentation you should be using “PUT” request type for your request 😉

So finally is it working? And for future response feel free to check them in [https://jsonpathfinder.com/](https://jsonpathfinder.com/)
