Table to JSON
Attempts to convert HTML tables into JSON.
Can be passed the markup for a single table as a string, a fragment of HTML or an entire page or just a URL (with an optional callback function; promises also supported).
The response is always an array. Every array entry in the response represents a table found on the page (in same the order they were found in the HTML).
Options
Tables with headings in the first column
If a table contains headings in the first column you might get an unexpected result, but you can pass a second argument with options with { useFirstRowForHeadings: true }
to have it treat the first column as it would any other cell.
tabletojson.convertUrl(
'https://www.timeanddate.com/holidays/ireland/2017',
{ useFirstRowForHeadings: true },
function(tablesAsJson) {
console.log(tablesAsJson);
}
);
Tables with HTML
The following options are true by default, which converts all values to plain text to give you an easier more readable object to work with:
- stripHtmlFromHeadings
- stripHtmlFromCells
If your table contains HTML you want to parse (for example for links) you can set stripHtmlFromCells
to false
to treat it as raw text.
tabletojson.convertUrl(
'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes',
{ stripHtmlFromCells: false },
function(tablesAsJson) {
console.log(tablesAsJson[1][0]);
}
);
Note: This doesn't work with nested tables, which it will still try to parse.
You probably don't need to set stripHtmlFromHeadings
to false (and setting it to false can make the results hard to parse), but if you do you can also set both at the same time by setting stripHtml
to false.
Duplicate column headings
If there are duplicate column headings, subsequent headings are suffixed with a count:
// Table
| PLACE | VALUE | PLACE | VALUE |
| abc | 1 | def | 2 |
// Example output
[{
PLACE: 'abc', VALUE: '1',
PLACE_2: 'def', VALUE_2: '2',
}]
Options forceIndexAsNumber
Instead of using column text (that sometime re-order the data), force an index as a number (string number).
{
"0": "",
"1": "A会",
"2": "B会",
"3": "C会",
"4": "Something",
"5": "Else",
"6": ""
}
Options, known issues and limitations
This module only supports parsing basic tables with a simple horizontal set of headings and corresponding cells.
It can give useless or weird results on tables that have complex structures (such as nested tables) or multiple headers (such as on both X and Y axis).
You'll need to handle things like work out which tables to parse and (in most cases) clean up the data. You might want to combine it it with modules like json2csv or CsvToMarkdownTable.
You might want to use it with a module like 'cheerio' if you want to parse specific tables identified by id or class (i.e. select them with cheerio and pass the HTML of them as a string).
Example usage
var tabletojson = require('tabletojson');
var tablesAsJson = tabletojson.convert(html);
var firstTableAsJson = tablesAsJson[0];
var secondTableAsJson = tablesAsJson[1];
...
var tabletojson = require('tabletojson');
var url = 'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes';
tabletojson.convertUrl(url, function(tablesAsJson) {
var listofSovereignStates = tablesAsJson[0];
});
var tabletojson = require('tabletojson');
var url = 'http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating';
tabletojson.convertUrl(url)
.then(function(tablesAsJson) {
var standardAndPoorRatings = tablesAsJson[1];
var fitchRatings = tablesAsJson[2];
});
var tabletojson = require('tabletojson');
var json2csv = require('json2csv');
var url = 'http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating';
tabletojson.convertUrl(url)
.then(function(tablesAsJson) {
var standardAndPoorCreditRatings = tablesAsJson[1];
json2csv({ data: standardAndPoorCreditRatings,
fields: [ 'Country', 'Outlook']
}, function(err, csv) {
console.log(csv);
});
});
Issues
Right now the table needs to be "well formatted" to be convertable. Tables in Html pages with not be processed.
<thead>
<tr>
<th>Header</th>
<tr>
</thead>
Contributing
Improvements, fixes and suggestions for better written modules that other people have created are welcome, as are bug
reports against specific tables it is unable to handle.
You can find basic tests in the test folder. I implemented the most straight forward way in using the library. Nonetheless
there are some edge cases that need to be tested and I would like to ask for support here. Feel free to fork and create
PRs here. Every bit of help is appreciated.
To get also an insight you can use Iain's examples located in the example folder included with this project that shows
usage and would be a good start.
If you submit a pull request, please add an example for your use case, so I can understand what you want it to do (as I
want to get around to writing tests for this and want to understand the sort of use cases people have).
Thanks
June 2018 - Very special thanks to the originator of the library, Iain Collins (@iaincollins). Without his investigation in website
grasping and mastering cheerio this lib would have not been where it is right now. Also I would personally like to say
"Thank you" for your trust in passing me the ownership. @maugenst
Special thanks to Marius Augenstein (@maugenst) for the latest major update, which includes ES6 syntax, uses native
promises and has much improved code and inline documentation.
Additional thanks to @roryok, Max Thyen (@maxthyen), Thor Jacobsen (@twjacobsen) and Michael Keller (@mhkeller) for
improvements and bug fixes.