Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Convert html tables to object (or array). Supports complex rowspan and colspan.

License

NotificationsYou must be signed in to change notification settings

coderosh/jsonfromtable

Repository files navigation

banner

Convert html tables to object (or array). Supports complex rowspan and colspan.

NPMMITCIPRs welcome!Typescript

Installation

Install via npm

npm install jsonfromtable

Or via yarn

yarn add jsonfromtable

Usage

From html string

  • Get array of objects with title as keys

    const{ JSONFromTable}=require('jsonfromtable')constobj=JSONFromTable.fromString(`<html>...</html>`)console.log(obj)/*[  {    title: value1,    title2: value2,    ...  },  ...]*/
  • Get array of title and body

    const{ JSONFromTable}=require('jsonfromtable')const{ headers, body}=JSONFromTable.arrayFromString(`<html>...</html>`)console.log(headers)// [title1, title2, title3, ...]console.log(body)// [ [val1, val2, ...], [val3, val4, ...], ... ]

From url

const{ JSONFromTable}=require('jsonfromtable')asyncfunctionmain(){constobj=awaitJSONFromTable.fromUrl(`https://...`)console.log(obj)const{ headers, body}=awaitJSONFromTable.arrayFromUrl(`https://...`)console.log(headers)console.log(body)}main()

Each function inJSONFromTable accepts two arguments. First is source (html string or url) and second isoptions.

interfaceOptions{titles?:string[]// custom titles (eg: ["sn", "name", "title"])firstRowIsHeading?:boolean// use first row for titles ?includeFirstRowInBody?:boolean// add first row in body ?tableSelector?:string// css selector for table (eg: table.wikitable)rowColSelector?:[string,string]// css selectors for row and col (eg: ["tr", "th,td"])shouldBeText?:boolean// if false value is html else truetrim?:boolean// should trim the value ?}

Example

conststr=`<table>  <tr>    <th>name</th>    <th>alias</th>    <th>class</th>    <th>info</th>  </tr>  <tr>    <td colspan="2">Roshan</td>    <td>Eng</td>    <td rowspan="2">na</td>  </tr>  <tr>    <td rowspan="2">John</td>    <td colspan="2">Cook</td>  </tr>  <tr>    <td rowspan="2">Danger</td>    <td colspan="2"> Ninja</td>  </tr>  <tr>    <td>AGuy</td>    <td>Eng</td>    <td rowspan="2">Eats a lot </td>  </tr>  <tr>    <td colspan="2"> Dante</td>    <td rowspan="2">Art</td>  </tr>  <tr>    <td>Jake</td>    <td>ake</td>    <td>Actor</td>  </tr></table>`constobj=JSONFromTable.fromString(str,{tableSelector:'table',trim:true,})console.log(obj)

License

MIT

About

Convert html tables to object (or array). Supports complex rowspan and colspan.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp