Looking to transform the provided page into a JSON object:
var mybody = document.getElementsByTagName('body')[0];
var table = mybody.getElementsByTagName('table')[0];
var jsonObj = [];
for (var i = 0, row; row = table.rows[i]; i++)
{
var cell2 = row.cells.length == '1' ? '' : row.cells[1].textContent.trim();
jsonObj.push( { what: row.cells[0].textContent.trim(), value:cell2 });
}
console.log(jsonObj);
This code generates the following JSON object
[
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "JU 109, JA 1376" },
Object { what= "Avio-kompanija" , value= "JAT AIRWAYS" },
Object { what= "Tip aviona" , value= "AT72" },
Object { what= "Planirano vrijeme" , value= "06:20" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" },
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "SOP 4121" },
Object { what= "Avio-kompanija" , value= "SOLINAIR LTD" },
Object { what= "Tip aviona" , value= "SF34" },
Object { what= "Planirano vrijeme" , value= "16:35" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" }
]
As the data is for 2 flights on the page, we need to include two elements in the JSON string describing
- Flight1
- Flight2
To achieve this structure:
{ "Flight1": [
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "JU 109, JA 1376" },
Object { what= "Avio-kompanija" , value= "JAT AIRWAYS" },
Object { what= "Tip aviona" , value= "AT72" },
Object { what= "Planirano vrijeme" , value= "06:20" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" }],
"Flight2": [
Object { what= "Beograd" , value= "" },
Object { what= "Broj leta" , value= "SOP 4121" },
Object { what= "Avio-kompanija" , value= "SOLINAIR LTD" },
Object { what= "Tip aviona" , value= "SF34" },
Object { what= "Planirano vrijeme" , value= "16:35" },
Object { what= "Status leta" , value= "Odletio" },
Object { what= "" , value= "" ]
}