Json to HTML 在 Dataweave 2.0 中

huangapple go评论70阅读模式
英文:

Json to HTML in Dataweave 2.0

问题

以下是您要求的部分翻译:

<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;">产品ID</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid blue; font-size: 14px;">产品编号</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">产品名称</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">产品优先级</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">产品附件</th>
    <th bgcolor="#6c7ae0" style="color: white !important; border: 1px solid black; font-size: 14px;">组合?</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>

希望这可以帮助您。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:

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:

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

The html output that I need is below:

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

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

答案1

得分: 2

DataWeave不支持HTML作为输出类型(查看此链接获取支持的格式)。但是,我们可以使用"application/xml"来准备带有必要标签的HTML内容,然后在发送电子邮件或到目标系统时将结果的内容类型设置为"text/html"。希望这些信息对您有帮助。

以下是翻译好的DataWeave脚本输出部分:

<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>
英文:

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

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

DataWeave Script

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

Output

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

huangapple
  • 本文由 发表于 2023年7月10日 16:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定