Hello,
I am trying to read data from a JSON file, but I am having trouble. I would like the background to change based on the level type indicated (deep, light, wake, rem). I followed Dan Shiffman’s Coding train video and have worked with JSON in a different project before, but for some reason it is not working this time. The error message I receive is “sleep[L].data is undefined”
Here is sample JSON file:
[{
"logId" : 27405388351,
"dateOfSleep" : "2020-05-30",
"startTime" : "2020-05-29T23:43:30.000",
"endTime" : "2020-05-30T07:53:00.000",
"duration" : 29340000,
"minutesToFallAsleep" : 0,
"minutesAsleep" : 434,
"minutesAwake" : 55,
"minutesAfterWakeup" : 1,
"timeInBed" : 489,
"efficiency" : 94,
"type" : "stages",
"infoCode" : 0,
"levels" : {
"summary" : {
"deep" : {
"count" : 3,
"minutes" : 37,
"thirtyDayAvgMinutes" : 57
},
"wake" : {
"count" : 33,
"minutes" : 55,
"thirtyDayAvgMinutes" : 68
},
"light" : {
"count" : 31,
"minutes" : 293,
"thirtyDayAvgMinutes" : 266
},
"rem" : {
"count" : 9,
"minutes" : 104,
"thirtyDayAvgMinutes" : 99
}
},
"data" : [{
"dateTime" : "2020-05-29T23:43:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-29T23:44:00.000",
"level" : "light",
"seconds" : 2250
},{
"dateTime" : "2020-05-30T00:21:30.000",
"level" : "deep",
"seconds" : 960
},{
"dateTime" : "2020-05-30T00:37:30.000",
"level" : "light",
"seconds" : 1560
},{
"dateTime" : "2020-05-30T01:03:30.000",
"level" : "rem",
"seconds" : 1500
},{
"dateTime" : "2020-05-30T01:28:30.000",
"level" : "light",
"seconds" : 1770
},{
"dateTime" : "2020-05-30T01:58:00.000",
"level" : "wake",
"seconds" : 360
},{
"dateTime" : "2020-05-30T02:04:00.000",
"level" : "light",
"seconds" : 3660
},{
"dateTime" : "2020-05-30T03:05:00.000",
"level" : "deep",
"seconds" : 840
},{
"dateTime" : "2020-05-30T03:19:00.000",
"level" : "light",
"seconds" : 180
},{
"dateTime" : "2020-05-30T03:22:00.000",
"level" : "rem",
"seconds" : 1740
},{
"dateTime" : "2020-05-30T03:51:00.000",
"level" : "light",
"seconds" : 360
},{
"dateTime" : "2020-05-30T03:57:00.000",
"level" : "rem",
"seconds" : 360
},{
"dateTime" : "2020-05-30T04:03:00.000",
"level" : "light",
"seconds" : 5310
},{
"dateTime" : "2020-05-30T05:31:30.000",
"level" : "rem",
"seconds" : 1320
},{
"dateTime" : "2020-05-30T05:53:30.000",
"level" : "wake",
"seconds" : 210
},{
"dateTime" : "2020-05-30T05:57:00.000",
"level" : "light",
"seconds" : 930
},{
"dateTime" : "2020-05-30T06:12:30.000",
"level" : "rem",
"seconds" : 330
},{
"dateTime" : "2020-05-30T06:18:00.000",
"level" : "wake",
"seconds" : 900
},{
"dateTime" : "2020-05-30T06:33:00.000",
"level" : "light",
"seconds" : 2490
},{
"dateTime" : "2020-05-30T07:14:30.000",
"level" : "deep",
"seconds" : 450
},{
"dateTime" : "2020-05-30T07:22:00.000",
"level" : "light",
"seconds" : 480
},{
"dateTime" : "2020-05-30T07:30:00.000",
"level" : "rem",
"seconds" : 1170
},{
"dateTime" : "2020-05-30T07:49:30.000",
"level" : "wake",
"seconds" : 210
}],
"shortData" : [{
"dateTime" : "2020-05-29T23:43:30.000",
"level" : "wake",
"seconds" : 90
},{
"dateTime" : "2020-05-29T23:48:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-29T23:50:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T01:00:30.000",
"level" : "wake",
"seconds" : 180
},{
"dateTime" : "2020-05-30T01:10:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T01:32:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T01:51:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T02:08:00.000",
"level" : "wake",
"seconds" : 60
},{
"dateTime" : "2020-05-30T02:20:00.000",
"level" : "wake",
"seconds" : 60
},{
"dateTime" : "2020-05-30T02:22:30.000",
"level" : "wake",
"seconds" : 150
},{
"dateTime" : "2020-05-30T02:28:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T02:43:30.000",
"level" : "wake",
"seconds" : 120
},{
"dateTime" : "2020-05-30T03:18:00.000",
"level" : "wake",
"seconds" : 60
},{
"dateTime" : "2020-05-30T03:22:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T03:38:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T03:50:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T04:04:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T04:24:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T04:26:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T04:30:00.000",
"level" : "wake",
"seconds" : 60
},{
"dateTime" : "2020-05-30T04:34:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T04:45:30.000",
"level" : "wake",
"seconds" : 60
},{
"dateTime" : "2020-05-30T04:53:00.000",
"level" : "wake",
"seconds" : 60
},{
"dateTime" : "2020-05-30T05:09:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T05:29:00.000",
"level" : "wake",
"seconds" : 120
},{
"dateTime" : "2020-05-30T05:42:30.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T05:59:00.000",
"level" : "wake",
"seconds" : 120
},{
"dateTime" : "2020-05-30T06:52:00.000",
"level" : "wake",
"seconds" : 30
},{
"dateTime" : "2020-05-30T07:27:30.000",
"level" : "wake",
"seconds" : 30
}]
},
"mainSleep" : true
}
Here is my code:
let L = 0;
let secs = 0;
// let list1 = [];
let sleep = [];
function preload(){
//Load list of json file names
//list1 = loadStrings('dataList.txt');
sleep = loadJSON("data/sleep-2020-04-30.json");
}
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
}
function draw() {
let sleepLevel = sleep[L]['data']['level'];
print(sleepLevel)
if (sleepLevel == ["wake"]){
//frameCount = 0;
background(0, 100, 100);
}
if (sleepLevel == ["deep"]){
background(100, 100, 100);
}
if (sleepLevel == ["light"]){
background(200, 100, 100);
}
if (sleepLevel == ["rem"]){
background(300, 100, 100);
}
L += 1;
}
``