After sending a http request to an api endpoint, I receive the following response:
{ "status": 200, "headers": "{\"server\":\"nginx\",\"date\":\"Sat, 13 Jun 2015 22:29:35 GMT\",\"content-type\":\"application/json; charset=utf-8\",\"content-length\":\"223\",\"connection\":\"keep-alive\",\"status\":\"200 OK\",\"cache-control\":\"no-cache, no-store, must-revalidate\",\"pragma\":\"no-cache\",\"x-frame-options\":\"SAMEORIGIN\",\"vary\":\"Accept-Encoding\",\"x-ua-compatible\":\"IE=Edge,chrome=1\",\"set-cookie\":[\"_twitch_session_id=4654465464564645645646546; domain=.twitch.tv; path=/; expires=Sun, 14-Jun-2015 10:29:35 GMT; HttpOnly\"],\"x-request-id\":\"lostsOfStringsStuffHere\",\"x-runtime\":\"0.403684\",\"accept-ranges\":\"bytes\",\"x-varnish\":\"1124564703\",\"age\":\"0\",\"via\":\"1.1 varnish\",\"x-mh-cache\":\"rails-varnish-6db1a8; M\",\"front-end-https\":\"on\"}", "body": "\"{\\"access_token\\":\\"lostsOfStringsStuffHere\\",\\"refresh_token\\":\\"lostsOfStringsStuffHere\\",\\"scope\\":[\\"user_read\\"]}\"" }
I proceed to deserialize the body of this response:
var data = JSON.parse(response.body);
The deserialized data looks like this:
{ "access_token":"lostsOfStringsStuffHere", "refresh_token":"lostsOfStringsStuffHere", "scope":["user_read"] }
However, when trying to access individual items within this object, they return as undefined
.
For example:
console.log(data.access_token); // undefined
Although I have experience with PHP's json_decode()
, working with json in JavaScript is new to me. I've searched for npm packages that can aid in handling json but haven't found one that suits my needs. Any recommendations?