3.1.2 • Published 2 years ago

jsonfromtable v3.1.2

Weekly downloads
11
License
MIT
Repository
github
Last release
2 years ago

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')
    
    const obj = 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')

async function main() {
  const obj = await JSONFromTable.fromUrl(`https://...`)
  console.log(obj)

  const { headers, body } = await JSONFromTable.arrayFromUrl(`https://...`)
  console.log(headers)
  console.log(body)
}

main()

Each function in JSONFromTable accepts two arguments. First is source (html string or url) and second is options.

interface Options {
  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 true
  trim?: boolean // should trim the value ?
}

Example

const str = `<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>`

const obj = JSONFromTable.fromString(str, {
  tableSelector: 'table',
  trim: true,
})

console.log(obj)

License

MIT

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.1.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago