英文:
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:
[
{
"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>
答案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
[
{
"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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论