Skip to content

Instantly share code, notes, and snippets.

@ccheney
Created December 27, 2012 15:38
Show Gist options
  • Save ccheney/4389114 to your computer and use it in GitHub Desktop.
Save ccheney/4389114 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){
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