Dataweave JSON au format XML-Mule-4
Je suis un débutant dans l'utilisation de la transformation dataweave, j'essaie d'écrire une expression dataweave Mule 4 pour convertir l'entrée JSON au format XML de sortie, j'ai une charge utile au format JSON et je souhaite la convertir dans un format XML spécifique, ci-dessous le JSON réel avec XML de sortie
La balise product-id (XML) proviendra de = attribut name de la balise PBSI__Item__r de JSON et la valeur d'allocation dans XML provient de l'attribut name de la balise 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"
}
]
XML de sortie:
<?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>
Réponses
Cela devrait vous aider à trouver une solution et à apprendre la façon de le faire, je n'ai pas donné de solution complète, de sorte que vous puissiez l'utiliser comme point de départ (presque 80% fait).
Aussi pour inclure l'espace de noms, suivez le lien 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()
}
})}
}
}
}