Skip to content

acagastya/jsonify-my-table

Repository files navigation

jsonify-my-table

Convert a HTML table to JSON. Actually, to an array of JSON objects.

TL;DR

const { res } = document
  .getElementById('tableID')
  .jsonify();

Usage

Consider a HTML table with id=myTable.

<table id="myTable">
  ...
</table>

To convert it to an array of JSON objects:

const table = document.getElementById('myTable');
const obj = table.jsonify();
const result = obj.res; // Array of objects.

jsonify only works on table (HTMLTableElement).

Data members

In the above example, obj contains following data members:

  • error — a flag which indicates if an error was occurred. It is false if no errors occurred, else contains the error message.
  • res — the array containing JSON objects.
  • headers — the keys in the objects (also, the table headers).
const table = document.getElementById('tableID');
const obj = table.jsonify();

if (!obj.error) {
  // obj.res...
}

Errors

  1. There are no rows in the table.
  2. Headers repeat.
  3. <td> has more than one child nodes.

Note

Use this for the client-side JavaScript. Though this module is published as a Node Package Module, it can not run on server-side JavaScript. This is because it makes use of document, which is not available in Node, as is.

To add it in the <script> tag, make use of Unpkg.

Example: <script src="unpkg.com/jsonify-my-table@latest/jsonify-my-table.min.js"></script>

Releases

No releases published

Packages

No packages published