Reputation: 934
I am doing a data transformation in Mule 3. I am updating an existing code where a lot of variables are declared inside the DataWeave 1.0
I want to add a field called "type__c" inside a declared variable.
my input payload is as below
{
"billofPlaning": {
"billcollection": {
"itemDetail": [
{
"comm": [
{
"commodity": {
"itemSequence": 1,
"equipmentPrefix": "ABCD",
"equipmentNumber": "1234"
}
},
{
"commodity": {
"itemSequence": 2,
"equipmentPrefix": "DEFG",
"equipmentNumber": "5678"
}
}
]
}
]
},
"equip": [
{
"equipment": {
"prefix": "ABCD",
"number": "1234",
"type": 2345
}
},
{
"equipment": {
"prefix": "HIJ",
"number": "7890",
"type": 234567
}
}
]
}
}
My existing DataWeave 1.0 script is this:
%dw 1.0
%output application/java
%var inputData=payload
%var payBill= inputData.billofPlaning.billcollection.itemDetail default [] map (billcode,indexOfbillcode) -> {
billcode: {
"field1": "",
"type__c": ""
}
---
paybill
I want to reuse the below DataWeave 2.0 code in above DataWeave 1.0 code for the field "type__c".
%dw 2.0
output application/json
var test= ((payload.billofPlaning.equip) filter (($.equipment.prefix == payload.billofPlaning.billcollection.itemDetail.comm[0].commodity.equipmentPrefix[0])
and ($.equipment.number == payload.billofPlaning.billcollection.itemDetail.comm[0].commodity.equipmentNumber[0])))
---
"type__c": test.equipment."type"[0]
Can we do it in some better way using DataWeave 1.0 (basically using any local variable or something inside the payBill variable that we declared in DataWeave 1.0)?
Upvotes: 1
Views: 582
Reputation: 25812
It is not clear what is the logic for your transformations and what is the expected result. If you want just to reuse the test
variable, the only difference for that particular one between DataWeave 1 and DataWeave 2 is that for the former the variable declaration uses a %
character. I added it to the script and removed the inputData variable that serves no purpose. I also fixed the script that was missing an end curly bracket.
%dw 1.0
%output application/java
%var test= ((payload.billofPlaning.equip) filter (($.equipment.prefix == payload.billofPlaning.billcollection.itemDetail.comm[0].commodity.equipmentPrefix[0])
and ($.equipment.number == payload.billofPlaning.billcollection.itemDetail.comm[0].commodity.equipmentNumber[0])))
%var payBill=(payload.billofPlaning.billcollection.itemDetail default [])
map (billcode,indexOfbillcode) -> {
billcode: {
"field1": "",
"type__c": test
}
}
---
payBill
Output:
[{billcode={field1=, type__c=[{equipment={prefix=ABCD, number=1234, type=2345}}]}}]
If you want to define a 'local' variable you can with the using
syntax to declare scoped variables. I don't see how that does make sense in this particular script. Usually it is used for simplifying access to nested items, which this script does not use.
Example:
%dw 1.0
%output application/java
%var payBill=(payload.billofPlaning.billcollection.itemDetail default [])
map (billcode,indexOfbillcode) ->
using ( test= ((payload.billofPlaning.equip) filter (($.equipment.prefix == payload.billofPlaning.billcollection.itemDetail.comm[0].commodity.equipmentPrefix[0])
and ($.equipment.number == payload.billofPlaning.billcollection.itemDetail.comm[0].commodity.equipmentNumber[0]))) )
{
billcode: {
"field1": "",
"type__c": test
}
}
---
payBill
I wouldn't call this an improvement.
The equivalent in DataWeave 2.0 is the do command which has improved functionality and better syntax.
Upvotes: 1