# JSON Response always sets variable as null, even though the response contains a value

**URL:** <https://community.postman.com/t/json-response-always-sets-variable-as-null-even-though-the-response-contains-a-value/39538>\
**Category:** Help Hub\
**Tags:** tests, variables, collections, json\
**Created:** [September 28, 2022, 6:13am UTC](https://community.postman.com/t/json-response-always-sets-variable-as-null-even-though-the-response-contains-a-value/39538 "2022-09-28T06:13:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjc22](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@kjc22](https://community.postman.com/u/kjc22)\
**Post date:** [September 28, 2022, 6:13am UTC](https://community.postman.com/t/json-response-always-sets-variable-as-null-even-though-the-response-contains-a-value/39538/1 "2022-09-28T06:13:56Z")

</div>

I am trying to execute a series of API calls using a unique identifier which is unknown at the start of the calls.

I can obtain the unique identifier in my first call, cannot get the returned value to set as the environment variable.

I have run a GET call to lookup the unique identifier, the returned result is below:  
[  
{  
“Key”: 4  
}  
]

Under the Tests tab I have defined:  
**const jsonResponse = pm.response.json();**  
**pm.environment.set(“Key”, pm.response.json().Key);**

I can see that it creates the environment variable, but the value set for the variable is “null” instead of “4” in the example above.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 28, 2022, 8:39am UTC](https://community.postman.com/t/json-response-always-sets-variable-as-null-even-though-the-response-contains-a-value/39538/2 "2022-09-28T08:39:12Z")

</div>

Hi @kjc22

I notice that you are using `pm.response.json().Key` inside the environment set, but in the line above you set that value to the variable name “jsonResponse” so you can just use `jsonResponse[0].key`

The bit that you are missing to make this work is the **array index**. Your response is enclosed in square brackets which means you are working inside of an array. The index of an array starts at 0 so you need to add this when you reference the response variable. Like this;

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

* * *

Your code would look something like this;

```auto
const jsonResponse = pm.response.json();
pm.environment.set(“Key”, jsonResponse[0].key);

```
