I need to take table rows and convert to JSON.
Any ideas? I have this code here but it does not work.
function tableToJSON(tableID) { return $(tableID + " tr").map(function (row) { return row.descendants().pluck("innerHTML"); }).toJSON(); }
stringify($("#emails_form"). serializeArray()); If you want to store formData in a JSON file, you need to post it to the server (e.g. per AJAX) and save it. But in that case, you can simply post the form und convert it to JSON on the server itself.
function tableToJson(table) { var data = []; // first row needs to be headers var headers = []; for (var i=0; i<table.rows[0].cells.length; i++) { headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,''); } // go through cells for (var i=1; i<table.rows.length; i++) { var tableRow = table.rows[i]; var rowData = {}; for (var j=0; j<tableRow.cells.length; j++) { rowData[ headers[j] ] = tableRow.cells[j].innerHTML; } data.push(rowData); } return data; }
Taken from John Dyer's Blog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With