How to add additional object inside array in dataweave. Please find the input and expected response. I stored the below input in flowVars
Input:
{
"calculate": [{
"rate": 10.4500,
"margin": 0.000,
"amount": 1000
}]
}
If the input amount is greater than 1000, add additional one more object along with the original one. Response should look like below
Response:
{
"calculate": [{
"actualRate": 10.4500,
"amount": 1000
},
{
"actualRate": 10.4500,
"amount": null
}]
}
Dataweave: (not sure how to add extra object in the response above)
%dw 1.0
%output application/java
---
{
calculate: flowVars.calculate map {
actualRate:$.rate,
amount:$.amount
}
}
Could anyone can help me with this. Thanks in advance.
I think this could do what you need:
%dw 1.0
%output application/java
---
flowVars.calculate map {
calculate:[
{
actualRate:$.rate,
amount:$.amount
},
{
actualRate:$.rate,
amount : null
}
] when $.amount >= 1000
otherwise [
{
actualRate:$.rate,
amount:$.amount
}
]
}
Related
I'm new to dataweave and trying to transform the array and iterate over the "||" values
Input:
[
{
"card":"VISA$$0.0||MASTER$$140.0"
},
{
"card":"VISA$$0.0||MASTER$$147.0"
}
]
The DataWeave code that I tried:
%dw 2.0
output application/json
---
"CardList":payload map (data,index) ->
{
(data.card splitBy "||" map {
"sur": $
})
}
Expected response :
{
"cardList": [
{
"card": "VISA$$0.0"
},
{
"card": "MASTER$$140.0"
},
{
"card": "VISA$$0.0"
},
{
"card": "MASTER$$147.0"
}
]
}
Someone one could you please assist me here on mule 3 and 4.
thanks in advance.
Try as below - iterating through splitBy values (||)
%dw 2.0
output application/json
---
"CardList": flatten(payload map
( ($.card splitBy "||") map(item,index) ->
{
card : item
}))
My request payload looks something like below:
{
"AllCodes": [
{
"Code1": "ABC"
},
{
"Code2": "TUV"
},
{
"Code3": "XYZ"
}
]
}
I have a function which will replace the values in the above with a certain value. The code that I've now is below:
%dw 1.0
%output application/json
%function LookUp(codes) codes mapObject {($$): $.code} //function to return string values for above codes
%var codeLookup = LookUp(sessionVars.OutputCodes)
%var FinalCodes = payload.AllCodes map codeLookup[$] joinBy ';'
--
FinalCodes
The output of sessionVars.OutputCodes is:
{
"CodeMaster": {
"PrimaryCodes": {
"PrimarySpecCodes": {
"ABC": {
"code": "ABC-String1"
},
"TUV": {
"code": "TUV-String2"
}
}
}
}
}
The output that I am expecting is:
"ABC-String1;XYZ-String2;XYZ"
As you can see above, since the function is returning the values for only "ABC" and "TUV" codes, my final output should have the original value if no function value found.
I was trying to use a default value before map operator. But it doesn't seem to work.
We get all the values for allCodes with $ pluck $, then if codes[$] is not found, we default to the value $. I believe you just need to add default $ to your original dataweave for it to work, but I gave a complete solution for other users on StackOverflow.
%dw 1.0
%output application/json
%var outputCodes =
{
"CodeMaster": {
"PrimaryCodes": {
"PrimarySpecCodes": {
"ABC": {
"code": "ABC-String1"
},
"TUV": {
"code": "TUV-String2"
}
}
}
}
}
%var allCodes =
{
"AllCodes": [
{
"Code1": "ABC"
},
{
"Code2": "TUV"
},
{
"Code3": "XYZ"
}
]
}
%var codes = outputCodes.CodeMaster.PrimaryCodes.PrimarySpecCodes mapObject {
($$): $.code
}
---
(flatten (allCodes.AllCodes map ($ pluck $))) map (
codes[$] default $
) joinBy ';'
this produces:
"ABC-String1;TUV-String2;XYZ"
There are various ways to solve this. One could be as follows:
%dw 1.0
%output application/json
%var payload2= { "CodeMaster": { "PrimaryCodes": { "PrimarySpecCodes": { "ABC": {"code": "ABC-String1"}, "TUV": {"code": "TUV-String2"} } } } }
%var codes = payload2.CodeMaster.PrimaryCodes.PrimarySpecCodes mapObject {
($$) : $.code
}
%var finalCodes = (payload.AllCodes map {
a: $ mapObject {
a1: codes[$] default $
}
}.a.a1) joinBy ";"
---
finalCodes
Hi I need to transform the following JSON object:
{
"products": [
{
"itemno": "123131",
"description" : "Big Widget",
"attributes": [
{
"color": [
{
"value": "Red",
"codeValue": "RED_NO2"
},
{
"value": "Blue Licorice",
"codeValue": "BLUE-355"
}
]
},
{
"chemicals": [
{
"value": "Red Phosphorous",
"codeValue": "RED_PHOS"
},
{
"value": "Chlorine Bleach",
"codeValue": "CHLRN_BLCH"
}
]
}
]
}
]
}
I am trying to transform this with each attribute having an array of values where their value is the codeValue and it's an array of those string values.
This is the desired output:
{
"products": [
{
"itemno": "123131",
"description: : "Big Widget",
"attributes": [
{
"color": ["RED_NO2", "BLUE-355"]
},
{
"chemicals": ["RED_PHOS", "CHLRN_BLCH"]
}
]
}
]
}
This is the Dataweave. I cannot determine how to get the attribute names (i.e. color, chemicals as keys with the desired data.
There is not a lot of good examples on transforming data with Dataweave and I have spent a lot of time trying to figure this out.
This is one of the dataweaves that got there somewhat, but isn't the solution:
%dw 1.0
%output application/json
---
payload.products map
{
"ItemNo" : $.sku,
"Desc" : $.description,
"Test" : "Value",
"Attributes" : $.attributes map
{
'$$' : $ pluck $.value
}
}
Your help is greatly appreciated.
You can do something like this:
%dw 1.0
%output application/json
%function attributeCodeValues(attributes)
attributes map ((attr) ->
attr mapObject ((values, descriptor) ->
{
(descriptor): values map $.codeValue
}
)
)
---
payload.products map {
"ItemNo" : $.sku,
"Desc" : $.description,
"Test" : "Value",
"Attributes" : attributeCodeValues($.attributes)
}
Input:-
[
{
"appName": "helloworld-1"
},
{
"appName": "helloworld-2"
}
]
Expected Output
{
"appList": [ "helloworld-1" , "helloworld-2" ]
}
Can anyone guide me for data weave script for this ?
According to the DataWeave Documentation, you can apply the multi value selector. Multi value selector returns an array with all the values whose key matches the expression.
%dw 1.0
%output application/json
---
{
appList : payload.*appName
}
I am trying to transform a JSON object to an XML object using Mule Dataweave. Below is the input JSON:
{
"root": {
"OBJECT1": {
"PARAM1": "PARAM1VALUE",
"PARAM2": "PARAM2VALUE"
},
"OBJECT2": [{
"KEY": "PARAMROWKEYVALUE1",
"VALUE": "PARAMROWVALUEVAL1"
}, {
"KEY": "PARAMROWKEYVALUE2",
"VALUE": "PARAMROWVALUEVAL2"
}],
"OBJECT3": {
"PARAM3": "PARAM3VALUE",
"PARAM4": "PARAM4VALUE"
}
}
}
I want to transform the above to the XML below:
<root>
<node1>PARAM1VALUE PARAM2VALUE</node1>
<args>
<paramrow>
<KEY>PARAMROWKEYVALUE1</KEY>
<VALUE>PARAMROWVALUEVAL1</VALUE>
</paramrow>
<paramrow>
<KEY>PARAMROWKEYVALUE2</KEY>
<VALUE>PARAMROWVALUEVAL2</VALUE>
</paramrow>
</args>
<details>
<PARAM3>PARAM3VALUE</PARAM3>
</details>
</root>
But, when I used this JSON as sample input, it throws an error saying "validate mapping":
%dw 1.0
%output application/xml
---
payload
How do I map these object elements to XML?
Try the following DataWeave
%dw 1.0
%output application/xml
---
{
root: {
node1: payload.root.OBJECT1.PARAM1 ++ ' ' ++ payload.root.OBJECT1.PARAM2,
args: {
(payload.root.OBJECT2 map {
paramrow: {
KEY: $.KEY,
VALUE: $.VALUE
}
})
},
details: {
(payload.root.OBJECT3 mapObject {
'$$': $
})
}
}
}
Consider using the official MuleSoft Developer docs.
A lot of DataWeave examples and the complete reference can be found there.
https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-examples#json-to-xml
https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation
#mlucas67, yes the dataweave expression works.
Here is another way which i have tried.
%dw 1.0
%input payload application/json
%output application/xml
---
{
root: {(payload map {
node1: $.OBJECT1.PARAM1 ++ ' ' ++ $.OBJECT1.PARAM2,
args: {
($.OBJECT2 map paramrow: $)
},
details:
PARAM3: $.OBJECT3.PARAM3
})
}
}
Thank you all commented on this post.