# Variable Value Assigned by Request Response Parsing

**URL:** <https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001>\
**Category:** Help Hub\
**Tags:** tests, variables, parse\
**Created:** [November 25, 2023, 6:39pm UTC](https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001 "2023-11-25T18:39:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![topstop](https://avatars.discourse-cdn.com/v4/letter/t/ac8455/32.png) [@topstop](https://community.postman.com/u/topstop)\
**Post date:** [November 25, 2023, 6:39pm UTC](https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001/1 "2023-11-25T18:39:59Z")

</div>

Variable passportToken is assigned to this request

```auto
{
  login(username: "unxx", password: "PWXX") {
    token
    status
    isPasswordExpired
  } 

```

Response is

```auto
{token: "xxxxx....", status: true, isPasswordExpired: null}

```

In TESTs

```auto
// Save the login response to a variable
var tokenStr = pm.response.json().data.login;

//parse out the Key 'token' and get its value
 const key = Object.keys(tokenStr)[0];
 passportToken = Object.key(tokenStr[0])

 //set key value to passportToken variable

```

I’m getting a TypeError Object.key is not a function and need help to parse out the value for the key ‘token’

How is the variable just set to token: “xxxxx…” ?

---

<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:** [November 25, 2023, 6:48pm UTC](https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001/2 "2023-11-25T18:48:35Z")

</div>

Hey @topstop 👋

What do you get printed in the Postman console from this?

```auto
console.log(pm.response.json().data.login);

```

From your response, it’s not showing the `data` object and a `login` property in the structure - Where is that coming from?

Should just having `pm.response.json().token` as the reference work here, if your response is the same as your example.

---

<div class="post-metadata">

**Author:** ![topstop](https://avatars.discourse-cdn.com/v4/letter/t/ac8455/32.png) [@topstop](https://community.postman.com/u/topstop)\
**Post date:** [November 26, 2023, 3:45pm UTC](https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001/3 "2023-11-26T15:45:03Z")

</div>

Updated the script by setting the variable as below:

```auto
// Save the login response to a variable
var passportToken = pm.response.json().data.login.token;

// Set the login response to passportToken Collection variable
pm.collectionVariables.set("passportToken", passportToken);

//print console token value
console.log(pm.response.json().data.login.token);

```

Console log shows the Variable is assigned to just the token value  
that is used in the subsequent request, working as expected

---

<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:** [November 26, 2023, 7:03pm UTC](https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001/4 "2023-11-26T19:03:08Z")

</div>

Does that now solve your issue?

---

<div class="post-metadata">

**Author:** ![topstop](https://avatars.discourse-cdn.com/v4/letter/t/ac8455/32.png) [@topstop](https://community.postman.com/u/topstop)\
**Post date:** [November 27, 2023, 12:15am UTC](https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001/5 "2023-11-27T00:15:15Z")

</div>

Yes both using pm.response.json().data.login.token and  
pm.response.json().data.login[“token”] syntax parsed the JSON data as expected

---

<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:** [November 27, 2023, 5:00am UTC](https://community.postman.com/t/variable-value-assigned-by-request-response-parsing/55001/6 "2023-11-27T05:00:41Z")

</div>

Closed on November 27, 2023, 5:00am UTC


