json - JavaScript objects returning undefined -


i'm sending http request api endpoint returns following:

{ "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 run following de-serialize body of response:

var response = json.parse(response.body); 

which gives me this:

{ "access_token":"lostsofstringsstuffhere", "refresh_token":"lostsofstringsstuffhere", "scope":["user_read"] }

but when try access individual items of object, undefined.

example:

console.log(response.access_token); // undefined 

i'm new working json using javascript , have in past used php's json_decode() function process json back-end projects.

i have done searching haven't come across npm package perfect dealing json. can suggest one?

you don't need additional library. javascript's built in json fine. issue string gets returned escaped, when json.parse on response.body result string.

you parse again: json.parse(json.parse(response.body)).access_token

however why body being returned string. issue api using or http request library. think string unnecessary.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -