Converts HTML tables to JSON objects
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).
This module only supports parsing basic tables with a simple horizontal set of
It can give useless results on tables that have complex structures 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.
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).
// Convert an HTML blob into an array of all the tables on the pagevar tabletojson = require'tabletojson';var tablesAsJson = tabletojsonconverthtml;var firstTableAsJson = tablesAsJson0;var secondTableAsJson = tablesAsJson1;
// Fetch a URL and parse all it's tables into JSON, using a callbackvar tabletojson = require'tabletojson';var url = '';tabletojsonconvertUrlurlvar listofSovereignStates = tablesAsJson0;;
// Fetch a URL and parse all it's tables into JSON, using promisesvar tabletojson = require'tabletojson';var url = '';tabletojsonconvertUrlurlthenvar standardAndPoorRatings = tablesAsJson0;var fitchRatings = tablesAsJson1;;
// Fetch a table from Wikipedia and combine with json2csv to convert to CSVvar tabletojson = require'tabletojson';var json2csv = require'json2csv';var url = '';tabletojsonconvertUrlurlthenvar standardAndPoorCreditRatings = tablesAsJson0;json2csv data: standardAndPoorCreditRatingsfields: 'Country' 'Outlook'console.logcsv;/* Example output"Country","Outlook""Abu Dhabi, UAE","Stable""Albania","Stable""Andorra","Negative""Angola","Stable""Argentina","Negative""Aruba","Stable""Australia","Stable""Austria","Negative""Azerbaijan","Positive"...*/;;
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.
If there is enough interest and I get some examples that I'll improve the code and actually write some tests.