VMware Cloud Community
lurims
Enthusiast
Enthusiast

How to convent HTML table to JSON

Unfortunately I do not have API version for a website but able to read web version as text (HTML content) and able to see the tags for table.  Could not find any API call in vRO to convert it to json, soemthing like we have it for xmltoJSON.  Any idea how I can decode the HTML table to JSON?

0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee

There is no out of the box HTML parser in vRO.

Depending on how complex are the tables you want to parse (eg. can there be nested tables, etc.) you may be able to write a simple HTML parser yourself, by scanning HTML input as string and handling table-related tags/attributes.

Note that, unlike XML or JSON, the HTML files could be a bit 'less structured'. For example, there is no guarantee that all opening HTML tags will have matching closing tag, and that's one of the reasons why parsing HTML in a robust way is harder than parsing XML / JSON.

0 Kudos
aamarques
Enthusiast
Enthusiast

That should work:

function tableToJSON(table) {

    var data = [];

    var headers = [];

    for (i=0;i<table.rows[0].cells.length; i++) {

        headers[i] = table.rows[0].cells[i].innerText;

    }

    for (i=1;i<table.rows.length;i++) {

        var tableRow = table.rows[i];

        var rowData = {};

        for (var j = 0; j < tableRow.cells.length; j++) {

            rowData[headers[j]] = tableRow.cells[j].innerText;

        }

        data.push(rowData);

    }

    return data;

}

0 Kudos