Variable Value Assigned by Request Response Parsing

Variable passportToken is assigned to this request

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

Response is

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

In TESTs

// 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…” ?

Hey @topstop :wave:

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

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.

Updated the script by setting the variable as below:

// 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

Does that now solve your issue?

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