Convert a HTML table to JSON
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Agastya 1306cb35c2
Explicit comparison of table row length
8 months ago
.prettierignore rename 2 years ago
CHANGELOG.md Explicit comparison of table row length 8 months ago
LICENSE Explicit comparison of table row length 8 months ago
README.MD Update readme 11 months ago
jsonify-my-table.js Explicit comparison of table row length 8 months ago
jsonify-my-table.min.js Explicit comparison of table row length 8 months ago
package.json Explicit comparison of table row length 8 months ago

README.MD

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>