Mule Dataweave Replace Null with Specific Value - mule

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

Related

How to iterate over the string using data weave 1.0 and data weave 2.0?

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
}))

Convert properties from properties file into json in Dataweave 2.0

How to convert properties from a properties file
creditmaster.metadata.AverageFicoScore=700
creditmaster.a.b.c=xyz
into this json format in a generic way
{
creditmasterMetaData: [
{
attributeKey: "AverageFicoScore",
attributeValue: 700
}
]
}
This script is generic in that it doesn't matter what are the parts of the key, it only groups by the first element (before of the first dot) and the key name after the last dot, it ignores everything in the middle:
%dw 2.3
output application/java
import * from dw::core::Strings
fun mapProperties(props) =
entriesOf(props) // since Mule 4.3 / DW 2.3
filter (substringAfter($.key, ".") startsWith "metadata.") // to filter keys with .metadata.
groupBy ((item, index) -> substringBefore(item.key, "."))
mapObject ((value, key, index) ->
(key): value map {
attributeKey: substringAfterLast($.key, "."),
attributeValue: if (isInteger($.value)) $.value as Number else $.value
}
)
---
mapProperties(payload)
Input file:
creditmaster.metadata.AverageFicoScore= 700
other.a.b= 123
creditmaster.a.b.c=xyz
something.metadata.another.maximum=456
creditmaster.metadata.different.minimum=500
Output (in JSON for clarity):
{
"something": [
{
"attributeKey": "maximum",
"attributeValue": "456"
}
],
"creditmaster": [
{
"attributeKey": "minimum",
"attributeValue": "500"
},
{
"attributeKey": "AverageFicoScore",
"attributeValue": "700"
}
]
}
One alternative is using the pluck function. It lets you iterate over an object receiving the entries.
If you have this input
{
"creditmaster": {
"metadata": {
"AverageFicoScore": "700",
"OtherData": "Some value"
}
}
}
with this transformation
{
creditmasterMetaData:
payload.creditmaster.metadata pluck ((value, key, index) ->
{
attributeKey: key,
attributeValue: value
}
)
}
you get this output
{
"creditmasterMetaData": [
{
"attributeKey": "AverageFicoScore",
"attributeValue": "700"
},
{
"attributeKey": "OtherData",
"attributeValue": "Some value"
}
]
}

Mule - Generate Payload in JSON format from HashMap <String, String> in the following format

Mule 3.9: How to generate the payload in JSON format from the HashMap type.
Transform the HashMap type payload to JSON array format. The sample data of HashMap
Key: Value
MK1 : T1
MK2 : T2
MK3 : T3
Target format to generate in Mule from DataWeave 1.0
{
"cars": [
{
"makeId": "MK1",
"makeName": "T1"
},
{
"makeId": "MK2",
"makeName": "T2"
},
{
"makeId": "MK3",
"makeName": "T3"
}
]
}
Is it possible to covert the HashMap of key and value (both string type) to be converted to the above defined JSON array format.
This script produces the expected result:
%dw 1.0
%output application/json
---
{
"cars": payload pluck {
makeId: $,
makeName: $$
}
}
Output:
{
"cars": [
{
"makeId": "T1",
"makeName": "MK1"
},
{
"makeId": "T3",
"makeName": "MK3"
},
{
"makeId": "T2",
"makeName": "MK2"
}
]
}
After working out a few details about map operator in Mule dataweave, i was able to resolve the issue using the below transformer.
%dw 1.0
%output application/json
"cars":
payload map {
makeId: $,
makeName: $$ }

Add Extra object inside loop in Mule Dataweave

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
}
]
}

JSON to XML Dataweave transformation with list objects

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.