I'm having a great deal of difficulty iterating over a simple nested json. For some reason I cannot seem to get my desired output. I am looking to iterate over :
{
"onShift": {
"fastTrack1": {
"name": "Bob, bob",
"shift": "7a-7p",
"service": "Fasttrack",
"spectra": "722413",
"office": "",
"cell": ""
},
"fastTrack2": {
"name": "Bill, Bill",
"shift": "7a-7p",
"service": "Fasttrack2",
"spectra": "54827",
"office": "",
"cell": "123-456-9090"
},
"incoming": {
"incoming_fastTrack1": {
"name": "Billy, Bob",
"shift": "7p-7a",
"service": "Fasttrack",
"spectra": "54821",
"office": "",
"cell": "123-456-8909"
},
"incoming_fastTrack2": {
"name": "Funny, Bob",
"shift": "7p-7a",
"service": "Fasttrack2",
"spectra": "3478",
"office": "",
"cell": ""
},
"shiftEnd": {
"ended_E_MD": {
"name": "Kissy Bob",
"shift": "7a-3p",
"service": "Area E",
"spectra": "3281",
"office": "",
"cell": "123-456-12345"
},
"ended_D_MD": {
"name": "funky bob",
"shift": "7a-3p",
"service": "Area D",
"spectra": "0003",
"office": "",
"cell": ""
},
"ended_DE_MD": {
"name": "Jimmy, Bob",
"shift": "10a-6p",
"service": "Area D-E",
"spectra": "0002",
"office": "",
"cell": ""
},
"ended_Tr_MD": {
"name": "Jim, Bob",
"shift": "8a-4p",
"service": "Triage",
"spectra": "0001",
"office": "",
"cell": ""
}
}
}
How I am trying to iterate is like this (one of the many way's I've tried):
$.getJSON("../data/json/erCall.json", function(data){
for (var i in data['onShift']){
var name = data['onShift'][i].name;
var spec = data['onShift'][i].service;
var shift = data['onShift'][i].shift;
var cell = data['onShift'][i].cell;
var off = data['onShift'][i].office;
$("#dataTargetOnCall").append('<tr><td>'+name+'</td><td>'+service+'</td>...etc');
}
});
I've also tried:
$.getJSON("../data/json/erCall.json", function(data){
for (var i in data){
for (var j in data[i]){
var name = data[i]['onShift'].name;
var spec = data[i]['onShift'].service;
var shift = data[i]['onShift'].shift;
var cell = data[i]['onShift'].cell;
var off = data[i]['onShift'].office;
... and so on
My intention is to iterate all of the listings under "onShift" and output all of the items (about 20) into a table. It basically outputs which doctors are on shift. I know how to format the append() function, however that's only after I am able to capture the data from the JSON of course.
What can I try next?