Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tabletojson

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tabletojson

Converts HTML tables to JSON objects

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11K
decreased by-8.82%
Maintainers
1
Weekly downloads
 
Created
Source

Table to JSON

Converts HTML tables to JSON. Can be passed an HTML blob or URL.

Can be passed the markup for a single table, a fragment of HTML or an entire page or a URL (with an optional callback function - also supports promises).

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).

I wrote this because I've been doing a lot of scraping for hackdays (etc).

Known issues / limitations

It only supports parsing basic tables with a simple horizontal set of headings and correspndoing cells.

It's intended for hackers, you'll need to handle things like work out which tables to parse and (in most cases) to 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 only ever parse specific tables in a blob of HTML.

Example usage

// Convert an HTML blob into an array of all the tables on the page
var tabletojson = require('tabletojson');
var tablesAsJson = tabletojson.convert(html);
var firstTableAsJson = tablesAsJson[0];
var secondTableAsJson = tablesAsJson[1];
...
// Fetch a URL and parse all it's tables into JSON, using a callback
var tabletojson = require('tabletojson');
var url = 'http://en.wikipedia.org/wiki/List_of_sovereign_states';
tabletojson.convertUrl(url, function(tablesAsJson) {
    var listofSovereignStates = tablesAsJson[0];
    ...
});
// Fetch a URL and parse all it's tables into JSON, using promises
var tabletojson = require('tabletojson');
var url = 'http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating';
tabletojson.convertUrl(url)
.then(function(json) {
    var firstTableAsJson = tablesAsJson[0];
    var secondTableAsJson = tablesAsJson[1];
    ...
});
// Fetch a table from Wikipedia and combine with json2csv to convert to CSV
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(json) {
    var standardAndPoorCreditRatings = tablesAsJson[0];
    json2csv({ data: standardAndPoorCreditRatings, fields: [ 'Country', 'Outlook'] }, function(err, csv) {
        console.log(csv);
        /* Example output
            "Country","Outlook"
            "Abu Dhabi, UAE","Stable"
            "Albania","Stable"
            "Andorra","Negative"
            "Angola","Stable"
            "Argentina","Negative"
            "Aruba","Stable"
            "Australia","Stable"
            "Austria","Negative"
            "Azerbaijan","Positive"
            ...
        */
    });
});

Keywords

FAQs

Package last updated on 17 Feb 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc