-2

I need to get the token only in a variable in JS.

    {
  "user": {
    "id": "string",
    "nickName": "string",
    "score": 0,
    "rank": 0
  },
  "token": "string"
}

This is my response saved in a variable but I only need to get the "String" value from the token

2 Answers2

1

If you have stored this response in a variable E.g.:

let response = {
  "user": {
    "id": "string",
    "nickName": "string",
    "score": 0,
    "rank": 0
  },
  "token": "string"
}

You can extract the value from the "token" property like this

let tokenFromObject = response.token

or

let tokenFromObject = response["token"]

or

let { token } = response
Levissie
  • 181
  • 1
  • 3
  • 12
0

You should be able to take the variable you got this json in and do this:

let a = {"user":{},"token": "string"}; // this is what you got

console.log(a["token"])  // a["token"] will give you "string"