# Set response value as Variable in Test

**URL:** <https://community.postman.com/t/set-response-value-as-variable-in-test/46213>\
**Category:** Help Hub\
**Tags:** tests, variables, collections\
**Created:** [April 11, 2023, 4:58pm UTC](https://community.postman.com/t/set-response-value-as-variable-in-test/46213 "2023-04-11T16:58:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brcox9090](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@brcox9090](https://community.postman.com/u/brcox9090)\
**Post date:** [April 11, 2023, 4:58pm UTC](https://community.postman.com/t/set-response-value-as-variable-in-test/46213/1 "2023-04-11T16:58:09Z")

</div>

I am trying to figure out how to set a response value as a variable using a Test script so I can pass that value along to another call. All the tutorials I’ve seen do this AFTER setting the value in a Pre-request script, and the logic provided for that doesn’t seem to if you’re not setting the value yourself.

The response I get when I make the API call is as follows:

 ![Screenshot 2023-04-11 at 12.52.11 PM](https://us1.discourse-cdn.com/getpostman/original/3X/d/7/d722505d83665b9bc9cce607156b3725a11b0668.png)  
 ![Screenshot 2023-04-11 at 12.53.28 PM](https://us1.discourse-cdn.com/getpostman/original/3X/3/f/3ff1f65c4f3b32d3a644579b1cef113b089bbbfe.png)

I’d like to set the “id” value, which is “164070” as a variable called folderID that gets passed along to the next call.

Right now I have the following Test code written, but it’s returning the entire response body as the folderID value.

 ![Screenshot 2023-04-11 at 12.56.18 PM](https://us1.discourse-cdn.com/getpostman/original/3X/c/d/cdb27e19858787c87f1fc229ce8e6a125a879375.png)

Any idea what I’m doing wrong?

---

<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:** [April 11, 2023, 5:25pm UTC](https://community.postman.com/t/set-response-value-as-variable-in-test/46213/2 "2023-04-11T17:25:23Z")

</div>

You’re using `pm.response.text()` here which isn’t required for a JSON response, as you can use `.json()` to get the data in a format that’s easily parsable.

This is what you would need to get the _first_ object’s `id` value:

```javascript
let response = pm.response.json();
pm.environment.set('folderID', response.data[0].id);

```

Ensure that you have an `environment` file created and selected or it will not capture the variable.

* * *

I wrote this a while ago about getting different values from a JSON response, it may help you here:

> [@Sharing tips and tricks with others in the Postman community](https://community.postman.com/t/sharing-tips-and-tricks-with-others-in-the-postman-community/5123/5):
>
> Extracting values from a JSON response I notice lots of questions from the community which all have a similar flavour - Users are having trouble extracting data from a JSON response body when the value is within an array. These are normally JavaScript related questions, more than specific Postman questions but I want to use this thread to offer more support around this problem. For these examples I’ll be using the [https://randomuser.me](https://randomuser.me) API, this returns lots of random user data, in a JSON form…

---

<div class="post-metadata">

**Author:** ![brcox9090](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@brcox9090](https://community.postman.com/u/brcox9090)\
**Post date:** [April 12, 2023, 12:58pm UTC](https://community.postman.com/t/set-response-value-as-variable-in-test/46213/3 "2023-04-12T12:58:31Z")

</div>

Thank you for the help! This worked exactly the way I’d expected!
