Live data from Hacker News

Table-to-JSON

lightswitch05.github.io

11–20 of 33 posts

Re: Table-to-JSON

#11
The non-webdev part of me is rather depressed that we've resorted to storing N typically homogeneous records in a way which results in the serialization of field names N times.

Wouldn't it be nicer to make the default format an object of arrays rather than an array of objects?

In before "but gzip!"

Re: Table-to-JSON

#12
post #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.

You could do what they're suggesting without being too clever. For instance, our dynatable plugin does it this way:

    table.find('thead tr').children('th,td')
But it could easily be something like:

    var headings = table.find('thead th');
    if (!headings.length) headings = table.find('th');
    if (!headings.length) headings = table.find('tr:first td');

Re: Table-to-JSON

#13
post #11

The non-webdev part of me is rather depressed that we've resorted to storing N typically homogeneous records in a way which results in the serialization of field names N times. Wouldn't it be nicer to make the default format an object of arrays rather than an array of objects? In before "but gzip!"

TSV or CSV?

Re: Table-to-JSON

#14
I'm having a hard time understanding why everyone's jazzed about this. When is this a useful thing to do? I've literally never once encountered a problem where this would be useful.

I'm not trying to sound harsh, I just don't understand when I'd ever need to do something like this.

Re: Table-to-JSON

#15
post #14

I'm having a hard time understanding why everyone's jazzed about this. When is this a useful thing to do? I've literally never once encountered a problem where this would be useful. I'm not trying to sound harsh, I just don't understand when I'd ever need to do something like this.

This library is like a building block to e.g. building your own interactive table functionality. Let's say you have an HTML table, and you want to allow users to dynamically search or sort the table. Step 1 could be to translate the table to JSON, step 2 would be do all your JS magic on the JSON data set, and step 3 would be write it back to the table. This approach is cleaner than trying to do a bunch of logic on the DOM text. So, this plugin is step 1 done for you.

Re: Table-to-JSON

#16
post #11

The non-webdev part of me is rather depressed that we've resorted to storing N typically homogeneous records in a way which results in the serialization of field names N times. Wouldn't it be nicer to make the default format an object of arrays rather than an array of objects? In before "but gzip!"

But to do easy and ~beautiful~ work on the objects (and to work on them the way most JS libraries expect data) you'd have to zipper the arrays together anyway.

Re: Table-to-JSON

#18
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!

It might be overkill if you don't need interactive features, but my ATable[1] library takes a 2-dimensional array representation of the data, and turns it into an HTML table. I will add support for an array of JSON objects as the data source soon.

[1] - http://jarwol.com/aTable/

Re: Table-to-JSON

#19
post #7

Earlier quoted context omitted.

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.

You could do what they're suggesting without being too clever. For instance, our dynatable plugin does it this way: table.find('thead tr').children('th,td') But it could easily be something like: var headings = table.find('thead th'); if (!headings.length) headings = table.find('th'); if (!headings.length) headings = table.find('tr:first td');

The HTML specification's algorithm for determining the applicable headers for a table cell might be useful:

http://www.whatwg.org/specs/web-apps/current-work/multipage/...

This was developed with reference to a corpus of real web tables.

Re: Table-to-JSON

#20
Can we go the other way? JSON to Table? That would make this a really useful library for saving table states.
Post reply on HN