1pratham
1pratham

Reputation: 113

Google App Script - Accessing JSON object

I am trying to access some fields of data from a JSON object using google app script and I'm getting the following error: Cannot read property "dateTime" from undefined.

var steps = UrlFetchApp.fetch("http://api.fitbit.com/1/user/-/activities/steps"
      + "/date/" + dateString +
                                 "/1d/15min.json", options);

and steps is {"activities-steps":[{"dateTime":"2014-03-28","value":"5336"}],"activities-steps-intraday":{"dataset":[{"time":"00:00:00","value":0},{"time":"00:15:00","value":0},{"time":"02:45:00","value":0},{"time":"03:00:00","value":0},{"time":"03:15:00","value":549},{"time":"03:30:00","value":333},{"time":"20:45:00","value":0},{"time":"21:00:00","value":0},{"time":"21:15:00","value":0},{"time":"21:30:00","value":0},{"time":"21:45:00","value":0},{"time":"22:00:00","value":0},{"time":"22:15:00","value":0},{"time":"22:30:00","value":0},{"time":"22:45:00","value":0},{"time":"23:00:00","value":0},{"time":"23:15:00","value":0},{"time":"23:30:00","value":0},{"time":"23:45:00","value":0}],"datasetInterval":15,"datasetType":"minute"}}

I got this by MailApp.sendEmail('[email protected]','Data',steps);

and the error is produced by executing following code:

var steps2 = JSON.parse(steps.getContentText()); 
var blah = steps2.activities-steps[0].dateTime;

or

var steps2 = JSON.parse(steps); 
var blah = steps2.activities-steps[0].dateTime;

Thank you!

Upvotes: 0

Views: 4435

Answers (1)

adrichman
adrichman

Reputation: 1235

you can't use a hyphen in dot notation. try steps2['activities-steps'][0].dateTime

Upvotes: 2

Related Questions