Live data from Hacker News

Table-to-JSON

lightswitch05.github.io

1–10 of 33 posts

Re: Table-to-JSON

#2
Awesome. Something else that would be nice is to be able to go from JSON to HTML table. There are a few libraries out there that do this but none seem to be updated or recent. Any suggestions would be greatly appreciated.

Nice work!

Re: Table-to-JSON

#3
Nice ! Could be used in a progressive enhancement strategy : render your html then extract the json out of it to feed your MVWhatever (Angularjs, Emberjs, Backbonejs, nameitjs) and let javascript take on for the cool client side stuff.

Re: Table-to-JSON

#4
post #2

Awesome. Something else that would be nice is to be able to go from JSON to HTML table. There are a few libraries out there that do this but none seem to be updated or recent. Any suggestions would be greatly appreciated. Nice work!

If you're using node.js/Express, you could do this using Jade: http://stackoverflow.com/questions/5204905/dynamic-html-page...

Re: Table-to-JSON

#5
It's a nice start to something that looks pretty useful. There are some things to consider though:

What if you have headings as the first column in each row? What if you have headings in the first column, and the first row?

Also, what is the use case for data overrides? In my opinion, it completely defeats the purpose when your intention is to take the contents of the table.

Re: Table-to-JSON

#7
post #5

It's a nice start to something that looks pretty useful. There are some things to consider though: What if you have headings as the first column in each row? What if you have headings in the first column, and the first row? Also, what is the use case for data overrides? In my opinion, it completely defeats the purpose when your intention is to take the contents of the table.

The code does not try to be clever:

    var getHeadings = function(table) {
      var firstRow = table.find("tr:first").first();
      return notNull(opts.headings) ? opts.headings : rowValues(firstRow);
    };
The code is also going to fall down if you have things like wide/high cells. But HTML tables are so flexible and so routinely abused, I guess the problem is too hard to be solved robustly with any generality.

Re: Table-to-JSON

#9
How hard do you think this would be to rewrite in node w/o jquery? As was noted I could see a lot of scrapping potential for this.

Re: Table-to-JSON

#10
post #5

It's a nice start to something that looks pretty useful. There are some things to consider though: What if you have headings as the first column in each row? What if you have headings in the first column, and the first row? Also, what is the use case for data overrides? In my opinion, it completely defeats the purpose when your intention is to take the contents of the table.

I've written a plugin that actually does this. I haven't had time to write the documentation yet, but our Dynatable plugin [1] has been in use for almost 2 years now.

It's an alternative to the Datatables plugin. All its logic works on the set of data in JSON, so it first translates an html table into JSON, then vice versa.

The cool thing about this process is, you can write your own translate step to easily work with e.g. JSON from an Ajax request. And you can write your own render step to work with e.g. a styled list instead of a table.

By default, the translate step translates an html table to a JSON object, naming the attributes by the table column headings (underscore format by default, but can be hyphenated, snake-case, or lower-case too).

[1] http://os.alfajango.com/dynatable

EDIT: If anyone is interested, the part of the plugin that essentially does the "table to JSON" part is the getFromTable function here: https://github.com/JangoSteve/jquery-dynatable/blob/master/j...

Other useful things this function does is, determines whether the data is a string, number, boolean, or date (or other user-defined type) to do proper column sorting logic, and much more.

EDIT 2: Also, I can't speak for the author of the table-to-json plugin, but for dynatable, it's useful to have data-override values for the cells for cases like when you have a date in "Jan 1, 2013" format, which is meaningless when doing filter or sorting operations on the JSON; i.e. you could use the data-override to populate the proper date format so JS knows how to process it.

Post reply on HN