Dataweave JSON ke format XML-Mule-4
Saya seorang pemula dalam menggunakan transformasi dataweave, saya mencoba menulis ekspresi dataweave Mule 4 untuk mengubah input JSON menjadi format output XML, saya memiliki muatan dalam format JSON dan saya ingin mengubahnya menjadi format XML tertentu, di bawah ini adalah JSON aktual bersama dengan XML keluaran
product-id (XML) tag akan berasal dari = name atribut dari PBSI__Item__r tag JSON dan nilai alokasi dalam XML berasal dari atribut nama tag PBSI__Inventory__r
JSON:
[
{
"PBSI__Item__r": {
"Id": null,
"type": "PBSI__PBSI_Item__c",
"Name": "116065"
},
"PBSI__Inventory__r": [
{
"Id": null,
"type": "PBSI__PBSI_Inventory__c",
"PBSI__Real_Quantity__c": "13.0"
}
],
"PBSI__Location__r": {
"Id": null,
"type": "PBSI__PBSI_Location__c",
"Name": "OB043"
},
"Id": null,
"type": "PBSI__Lot__c"
},
{
"PBSI__Item__r": {
"Id": null,
"type": "PBSI__PBSI_Item__c",
"Name": "116066"
},
"PBSI__Inventory__r": [
{
"Id": null,
"type": "PBSI__PBSI_Inventory__c",
"PBSI__Real_Quantity__c": "1.0"
}
],
"PBSI__Location__r": {
"Id": null,
"type": "PBSI__PBSI_Location__c",
"Name": "OA011"
},
"Id": null,
"type": "PBSI__Lot__c"
}
]
keluaran XML:
<?xml version='1.0' encoding='UTF-8'?>
<inventory xmlns="http://www.demandware.com/xml/impex/inventory/2007-05-31">
<inventory-list>
<header list-id="Hastens_Inventory">
<default-instock>false</default-instock>
<use-bundle-inventory-only>false</use-bundle-inventory-only>
</header>
<records>
<record product-id="116065">
<allocation>13</allocation>
<allocation-timestamp>2019-04-24T07:09:51.954Z</allocation-timestamp>
</record>
<record product-id="116066">
<allocation>1</allocation>
<allocation-timestamp>2019-04-24T07:09:51.965Z</allocation-timestamp>
</record>
</records>
</inventory-list>
</inventory>
Jawaban
Ini akan membantu Anda mendapatkan solusi dan juga mempelajari cara melakukannya, saya belum memberikan solusi pukulan penuh, sehingga Anda dapat menggunakan ini sebagai titik awal (hampir 80% selesai).
Selain memasukkan namespace, ikuti tautannya https://docs.mulesoft.com/mule-runtime/4.3/dataweave-cookbook-include-xml-namespaces
{
inventory:{
inventorylist: {
header @("list-id":"Hastens_Inventory"):
{
"default-instock":false,
"use-bundle-inventory-only":false
},
records: {(payload map
{
record @("product-id": $.PBSI__Item__r.Name): { allocation: $.PBSI__Inventory__r[0].PBSI__Real_Quantity__c,
"allocation-timestamp": now()
}
})}
}
}
}