Skip to content

Instantly share code, notes, and snippets.

@pranid
Forked from ccheney/gist:4389114
Last active December 16, 2021 19:00
Show Gist options
  • Save pranid/3733d13be5360b231d0aaf4c9ffaad67 to your computer and use it in GitHub Desktop.
Save pranid/3733d13be5360b231d0aaf4c9ffaad67 to your computer and use it in GitHub Desktop.
jQuery: Convert HTML Table to JSON
(function($){
var convertTableToJson = function()
{
var rows = [];
$('table tr').each(function(i, n){
// Ignore empty
if(i != 0) {
var $row = $(n);
rows.push({
display_name: $row.find('td:eq(0)').text(),
first_name: $row.find('td:eq(1)').text(),
last_name: $row.find('td:eq(2)').text(),
street: $row.find('td:eq(3)').text(),
city: $row.find('td:eq(4)').text(),
state: $row.find('td:eq(5)').text(),
zip: $row.find('td:eq(6)').text()
});
}
});
return JSON.stringify(rows);
};
$(function(){
console.log(convertTableToJson ());
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment