Reputation: 59
I am a beginner in using dataweave transformation, I am trying to write a Mule 4 dataweave expression to convert input JSON into output XML format, I have a payload in JSON format and I want to convert it into a specific XML format, below is the actual JSON along with output XML
product-id(XML) tag will come from = name attribute of PBSI__Item__r tag of JSON and allocation value in XML comes from name attribute of PBSI__Inventory__r tag
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"
}
]
output 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>
Upvotes: 1
Views: 1778
Reputation: 4303
This should help you get to a solution and as well learn the way to do it, I haven't given out a full blow solution, so that you could use this as a starting point (almost 80% done).
As well to include the namespace, follow the link 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()
}
})}
}
}
}
Upvotes: 1