Search code examples
javascriptjsonjavascript-objects

JSON object name is undefined, how to access the parameters?


I am sending a request to a web server and get the below response:

Using JSON.stringify it looks as follows:

undefined{\"access_token\":\"Rhazjww5 ...QUiTMVc\",\"token_type\":\"bearer\",\"expires_in\":86399,\".issue
d\":\"Thu, 16 Aug 2018 13:50:28 GMT\",\".expires\":\"Fri, 17 Aug 2018 13:50:28 G
MT\"}"

I need the access_token part of the object, but do not know how to access it. I tried var.access_token and var['access_token'], where var is the variable I stored the object.

How can I access the data in the object?

Thank you


Solution

  • don't stringify already stringified data, use JSON.parse for parse string to js object

    const obj = JSON.parse(response);
    console.log(obj['access_token']);