html-table-to-json

1.0.0 • Public • Published

html-table-to-json Build Status

Extracts tables from a provided html snippet and converts them to JSON objects

Install

$ npm install html-table-to-json

Usage

const HtmlTableToJson = require('html-table-to-json');
 
const jsonTables = HtmlTableToJson.parse(`
        <table>
            <tr>
                <th>Animal</th>
                <th>Color</th>
                <th>Name</th>
            </tr>
            <tr>
                <td>Unicorn</td>
                <td>Pink</td>
                <td>Billy</td>
            </tr>
            <tr>
                <td>Walrus</td>
                <td>Orange</td>
                <td>Sue</td>
            </tr>
        </table>
    `);
 
console.log(jsonTables.results);
/* => [[
 *      {Animal: 'Unicorn', Color: 'Pink', Name: 'Billy'},
 *      {Animal: 'Walrus', Color: 'Orange', Name: 'Sue'}
 *    ]]
 */
 
console.log(jsonTables.count);
// => 1
 

API

HtmlTableToJson.parse(input [,options])

input

Type: string

Any html snippet.

options

Type: object

values

Type: bool

Return table rows as value arrays:

// HtmlTableToJson.parse(html, { values: true })
/* => [[
 *      ['Unicorn', 'Pink', 'Billy'],
 *      ['Walrus', 'Orange', 'Sue']
 *    ]]
 */

Headers

HtmlTableToJson extracts table headers ( th ) to be used as JSON object keys. The first row is used when no th elements are present.

License

MIT © Brandon Smith

Readme

Keywords

Package Sidebar

Install

npm i html-table-to-json

Weekly Downloads

3,334

Version

1.0.0

License

MIT

Unpacked Size

5.76 kB

Total Files

5

Last publish

Collaborators

  • brandon93s