Saturday 6 May 2017

Convert a "Complex" HTML Table to JSON

So the last few days i have been struggling to find a working solution to this problem.(NodeJS)

I already tried things like cheerio html tables etc but most of them dont work cause they dont handle the rowspans etc.

Basically i need a HTML table like this :

<table class="k" border-width="3">
<tbody>
    <tr>
        <th width="50">
            XXXXXX </th>
        <th width="50">
            YYYY</th>
        <th width="50">
            </th>
        <th width="50">
            ZZZZZZZZ</th>
        <th width="50">
            QQQQ</th>
        <th width="50">
            LLLL</th>
        <th width="150">
        </th>
    </tr>
    <tr class="k">
        <td rowspan="11" class="k">
            11</td>
        <td>
            Data</td>
        <td>
            1</td>
        <td>
            Data2</td>
        <td>
        </td>
        <td>
            Data3</td>
        <td>
            Data4</td>
    </tr>
    <tr>
        <td>
           Data5</td>
        <td>
            2</td>
        <td>
            &nbsp;</td>
        <td>
            Data6</td>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;Data7</td>
    </tr>
    <tr>
        <td>
            &nbsp;Data8</td>
        <td>
            2</td>
        <td>
            &nbsp;</td>
        <td>
        </td>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;Data10</td>
    </tr>
    <tr>
        <td>
            &nbsp;Foo</td>
        <td>
            3</td>
        <td>
            &nbsp;Foo</td>
        <td>
        </td>
        <td>
            &nbsp;Foo</td>
        <td>
            &nbsp;Foo</td>
    </tr>
    <tr>
        <td>
            &nbsp;Foo</td>
        <td>
            3</td>
        <td>
            &nbsp;Foo</td>
        <td>
        </td>
        <td>
            &nbsp;Foo</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;Foo</td>
        <td>
            4</td>
        <td>
            &nbsp;Foo</td>
        <td>
        </td>
        <td>
            &nbsp;Foo</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;Foo</td>
        <td>
            4</td>
        <td>
            &nbsp;</td>
        <td>
        </td>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;Foo</td>
    </tr>

</tbody>

to convert to something like this:

{
"XXXXXX": "11",
"YYYY": "Data",
"SSS": "1",
"ZZZZZZZZ": "Data2",
"QQQQ": "",
"LLLL": "Data3",
"Data4": ""}

for each row in the table



via Denis B.

No comments:

Post a Comment