HTML Table to JSON

May 5, 2010 | HTML/CSS, JavaScript | 6 Comments

I am updating a tool that allows users to customize a large data table to their needs. It’s sort of like a progressive insurance for seminaries in that it helps students compare information on about 30 US seminaries so they can make an informed decision about where to go.

My initial approach when I built it 4-5 years ago was to embed JSON data in the HTML page and then use JavaScript to convert that JSON data into a table based on the user’s choices.

But there are a few problems with this approach.

  1. The data is not accessible to screen readers.
  2. The data is not accessible to those without JavaScript
  3. Embedding a lot of JSON data in an HTML page isn’t a very good idea. It should be a separate cached .js file, but I can’t do

    that since the data often changes and it still wouldn’t address #1 and #2

So I’ve decided in the new version to start with a large <table> and then turn that table into JSON data using the <thead> region as the JSON labels. Then I can take that data and customize it based on the user’s choices. Interestingly the overall data size is a bit smaller since writing a <td> and </td> for each peice of information is actually smaller than writing the header name and quotes over and over.

Here’s the simple table to JSON code. It assumes the first row is has header names and removes spaces and lower cases it.

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; 

Below are links to the two pages for comparison. Please note that the site where the “new” page is is still a work in progress and won’t be launched for a month or two.

6 responses to “HTML Table to JSON”

  1. Devvyn P M says:

    Someone added a few lines of whitespace and posted this as an answer to a question on StackOverflow. I just thought you might find that interesting.

  2. Daniel says:

    I needed this, but with some added functionality like ignoring columns and over-ridding column names. I ended up writing a jQuery plugin for it. It is hosted here if anyone else needs it:

  3. Kiser says:

    You sir, are a scholar and a gentleman. And I do not throw those terms around lightly.

  4. Mahesh says:

    Hi i’m trying to work with your example. Its just reading the first column and ignoring the rest of the columns. I tried to change allowHTML: true in the jquery.tabletojson.js file and tried to run my application.

    Still i’m able to read only the first column itself. Can you let me know whether the problem exists or do i need to change any other values.

  5. cool says:

    superb article

  6. Ed Clifton says:

    Day-after-day We are learning a little bit more about NodeJS. It truly is definately the only real programming language to discover long-term. Just accomplished the BackSpace.Academy Amazon web services (aws) Certified Developers educational program. They have unfolded a totally new Realm of applications for me. Now I must spare the time in order to get developing!

Hi, I'm John Dyer. In my day job, I build websites and create online seminary software for a seminary in Dallas. I also like to release open source tools including a pretty popular HTML5 video player and build tools that help people find best bible commentaries and do bible study. And just for fun, I also wrote a book on the theology of technology and media.

Fork me on GitHub

Social Widgets powered by