jsonify-my-table

Convert a HTML table to JSON
git clone http://git.hanabi.in/repos/jsonify-my-table.git
Log | Files | Refs | LICENSE

README.MD (1528B)


      1 # jsonify-my-table
      2 
      3 Convert a HTML table to JSON. Actually, to an array of JSON objects.
      4 
      5 ## TL;DR
      6 
      7 ```javascript
      8 const { res } = document
      9   .getElementById('tableID')
     10   .jsonify();
     11 ```
     12 
     13 ## Usage
     14 
     15 Consider a HTML table with `id=myTable`.
     16 
     17 ```html
     18 <table id="myTable">
     19   ...
     20 </table>
     21 ```
     22 
     23 To convert it to an array of JSON objects:
     24 
     25 ```javascript
     26 const table = document.getElementById('myTable');
     27 const obj = table.jsonify();
     28 const result = obj.res; // Array of objects.
     29 ```
     30 
     31 `jsonify` only works on table (`HTMLTableElement`).
     32 
     33 ## Data members
     34 
     35 In the above example, `obj` contains following data members:
     36 
     37 - **`error`** &mdash; a flag which indicates if an error was occurred. It is `false` if no errors occurred, else contains the error message.
     38 - `res` &mdash; the array containing JSON objects.
     39 - `headers` &mdash; the keys in the objects (also, the table headers).
     40 
     41 ```javascript
     42 const table = document.getElementById('tableID');
     43 const obj = table.jsonify();
     44 
     45 if (!obj.error) {
     46   // obj.res...
     47 }
     48 ```
     49 
     50 ## Errors
     51 
     52 1. There are no rows in the table.
     53 2. Headers repeat.
     54 3. `<td>` has more than one child nodes.
     55 
     56 ## Note
     57 
     58 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.
     59 
     60 To add it in the `<script>` tag, make use of [Unpkg](https://unpkg.com 'UNPKG').
     61 
     62 **Example:** `<script src="unpkg.com/jsonify-my-table@latest/jsonify-my-table.min.js"></script>`