Search code examples
htmljsonmuledataweavemulesoft

Json to HTML in Dataweave 2.0


I am trying to convert a json array to html (in a tabular format) using dataweave 2.0 in MuleSoft. My input payload is something like this:

[
  {
    "Product ID": "4d5c0b68",
    "Product Number": "be56",
    "Product Name": "Product A",
    "Product Priority": "High Risk",
    "Product Attachments": 2,
    "Combo?": "No"
  }
]

The html output that I need is below:

<table style="width: 50%; border: 1px solid black; font-family: Monospace">
  <tr bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">
    <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product ID</th>
    <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid blue; font-size:14px; ">Product Number</th>
    <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product Name</th>
    <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product Priority</th>
    <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product Attachments</th>
    <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Combo?</th>
  </tr>
  <tr align="center" style="color: #666666; font-size:12px; border: 1px solid black; font-weight: 500; width:10%">
    <td style="color: black !important;border: 1px solid black; font-size:14px; ">4d5c0b68</td>
    <td style="color: black !important;border: 1px solid black; font-size:14px; ">be56</td>
    <td style="color: black !important;border: 1px solid black; font-size:14px; ">Product A</td>
    <td style="color: black !important;border: 1px solid black; font-size:14px; ">High Risk</td>
    <td style="color: black !important;border: 1px solid black; font-size:14px; ">2</td>
    <td style="color: black !important;border: 1px solid black; font-size:14px; ">No</td>
  </tr>
</table>

The json data currently given is for only 1 row and I will have to map the whole array in the html. Can this be achieved using dataweave 2.0?

This is what I have currently.

%dw 2.0
output application/xml writeDeclaration=false
var tableData = payload map ((item, index) -> 
{
    "Product ID" : item.ProductID,
    "Product Number": item.ProductNumber,
    "Product Name": item.ProductName,
    "Product Priority": item.ProductPriority,
    "Product Attachments": item.ProductAttachments as Number,
    "Combo?" : if (item.Combo == "false") "No" else "Yes"
})
var headerRow = [
    {
        "header": "Product ID",
        "style": "color: white !important; border: 1px solid black; font-size: 14px;"
    },
    {
        "header": "Product Number",
        "style": "color: white !important; border: 1px solid black; font-size: 14px;"
    },
    {
        "header": "Product Name",
        "style": "color: white !important; border: 1px solid black; font-size: 14px;"
    },
    {
        "header": "Product Priority",
        "style": "color: white !important; border: 1px solid black; font-size: 14px;"
    },
    {
        "header": "Product Attachments",
        "style": "color: white !important; border: 1px solid black; font-size: 14px;"
    },
    {
        "header": "Combo?",
        "style": "color: white !important; border: 1px solid black; font-size: 14px;"
    }
]
---
<table style="width: 50%; border: 1px solid black; font-family: Monospace">
  <tr bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">Case #</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid blue; font-size: 14px;">Interaction #</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">Product Name</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">PQC Priority</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">No. of PQC Attachments</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">Combo?</th>
  </tr>
  <tr align="center" style="color: #666666; font-size: 12px; border: 1px solid black; font-weight: 500; width: 10%">
    <td style="color: black !important; border: 1px solid black; font-size: 14px;">{(tableData[0]."Product ID")}</td>
    <td style="color: black !important; border: 1px solid black; font-size: 14px;">{(tableData[0]."Product Number")}</td>
    <td style="color: black !important; border: 1px solid black; font-size: 14px;">{(tableData[0]."Product Name")}</td>
    <td style="color: black !important; border: 1px solid black; font-size: 14px;">{(tableData[0]."Product Priority")}</td>
    <td style="color: black !important; border: 1px solid black; font-size: 14px;">{(tableData[0]."Product Attachments")}</td>
    <td style="color: black !important; border: 1px solid black; font-size: 14px;">{(tableData[0]."Combo?")}</td>
  </tr>
</table>

Solution

  • DataWeave does not support HTML as an output type (Check this for supported formats). However, we can use "application/xml" to prepare the HTML content with the necessary tags, and then output the result with the content type set to "text/html" when sending emails or to the target system. I hope this information is helpful.

    Input

    [
      {
        "Product ID": "4d5c0b68",
        "Product Number": "be56",
        "Product Name": "Product A",
        "Product Priority": "High Risk",
        "Product Attachments": 2,
        "Combo?": "No"
      },
      {
        "Product ID": "4d5c0b68",
        "Product Number": "be56",
        "Product Name": "Product A",
        "Product Priority": "High Risk",
        "Product Attachments": 2,
        "Combo?": "No"
      }
    ]
    

    DataWeave Script

    %dw 2.0
    var keys = keysOf(payload[0]) // Headers
    output application/xml writeDeclaration=false
    ---
    table @(style:"width: 50%; border: 1px solid black; font-family: Monospace"): {
        //Headers Mapping
        tr @(bgcolor:"#6c7ae0", style:"color: white !important;border: 1px solid black; font-size:14px; "): {
            (keys map ((item, index) -> {
                th @(bgcolor: "#6c7ae0", style: "color: white !important;border: 1px solid black; font-size:14px; "):item
            }))
        },
        //Values Mapping
        (payload map ((item, index) -> {
            tr @(align:"center", style:"color: #666666; font-size:12px; border: 1px solid black; font-weight: 500; width:10%"): {
                (item mapObject {
                    td @(style:"color: black !important;border: 1px solid black; font-size:14px; ", ): $
                })
            }
        }))
    }
    

    Output

    <table style="width: 50%; border: 1px solid black; font-family: Monospace">
      <tr bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">
        <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product ID</th>
        <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product Number</th>
        <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product Name</th>
        <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product Priority</th>
        <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Product Attachments</th>
        <th bgcolor="#6c7ae0" style="color: white !important;border: 1px solid black; font-size:14px; ">Combo?</th>
      </tr>
      <tr align="center" style="color: #666666; font-size:12px; border: 1px solid black; font-weight: 500; width:10%">
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">4d5c0b68</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">be56</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">Product A</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">High Risk</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">2</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">No</td>
      </tr>
      <tr align="center" style="color: #666666; font-size:12px; border: 1px solid black; font-weight: 500; width:10%">
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">4d5c0b68</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">be56</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">Product A</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">High Risk</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">2</td>
        <td style="color: black !important;border: 1px solid black; font-size:14px; ">No</td>
      </tr>
    </table>