Skip to content

Instantly share code, notes, and snippets.

@AmrEldib
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmrEldib/0ce79b812c1854bb37fb to your computer and use it in GitHub Desktop.
Save AmrEldib/0ce79b812c1854bb37fb to your computer and use it in GitHub Desktop.
Convert HTML Table to JSON Array
// Make sure to rename the object fields
// Table's header gets ignored
var tbl = $('table#tableToConvert tr:has(td)').map(function(i, v) {
var $td = $('td', this)
return {
wkid: $td.eq(0).text(),
name: $td.eq(1).text()
}
}).get();
$('#result').html(JSON.stringify(tbl));
// Sample HTML snippet
// <table id="tableToConvert" cellspacing="0">
// <thead>
// <tr>
// <th class="tableheader">Well-known ID</th>
// <th class="tableheader">Name</th>
// </tr>
// </thead>
// <tbody>
// <tr>
// <td> 103971</td>
// <td>NAD_1983_HARN_Adj_WI_Wood_Feet</td>
// </tr>
// </tbody>
// </table>
// <p id="result"></p>
// Source: http://stackoverflow.com/a/12291010/463
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment