'Mulesoft : HTML to JSON conversion?

Is it possible to convert an html input to JSON using mulesoft?

For my case specifically, I am trying to convert an HTML table to JSON arrays.

Input:

<table>
<tr><td>id</td><td>value1</td><td>value2</td></tr>
<tr><td>0 </td><td>0     </td><td>0     </td></tr>
<tr><td>0 </td><td>1.5   </td><td>2.15  </td></tr>
</table>

Output:

"JsonOutput" :[
  {id:"0",value1:"0",value2:"0"},
  {id:"1",value1:"1.5",value2:"2.15"}
]


Solution 1:[1]

What you can do is fetch the top tr element from the table and use it to determine the headers. Then you can create the JSON dynamically using that headers array.

%dw 2.0
var tableAsArray = payload.table.*tr
var tableHeaderRow = tableAsArray[0].*td
var tableValuesRow = tableAsArray[1 to -1]
output application/json  
---
{
  "JsonOutput": tableValuesRow map ((tr) -> {
    (tr.*td map ((td, index) -> {
      (tableHeaderRow[index]): td
    }))
  })
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harshank Bansal