/table-to-json

table to json in javascript

Primary LanguageJavaScript

tableToJSON

Converts a table from an HTML document to a JSON object.

Parameters

  • table: the table element to be converted.
  • columnNames (optional): an array of strings to use as column headers. If not provided, column headers will be extracted from the table.

Returns

An object with the following properties:

  • rowHeaders: an array of row headers (strings).
  • columnHeaders: an array of column headers (strings).
  • data: an array of objects representing the table's rows, where each object's keys are the column headers and the values are the corresponding cell values.

Usage

import { tableToJSON } from 'tableToJSON';

let table = document.querySelector('table');
let json = tableToJSON(table);
console.log(json);


## Problems

* Doesn’t handle tables with `colspan` or `rowspan`
* Doesn’t handle tables with `th`s beyond the first `tr`.