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

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: $$ }

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

Mule Dataweave Replace Null with Specific Value

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

Convert list of JSON Object to single list on Mule DataWeave

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
}

Lookup list of Maps variable in data weave script

I have a list of maps (listOfMapsObject) like below
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
I would like access "Value" field for a given Id in dataweave script.
For example: For Id = 1234, Text1 should be returned.
%dw 1.0
%output application/json
%var listOfMapsObject = flowVars.listOfMaps
---
payload map {
"key" : $.key,
"data" : lookup Value field in listOfMapsObject by given key
}
Approch suggested by #'sulthony h' is fine but it will end up in performance issue if you large number of data in pyload and listOfMapsObject. As filter is used , for each record of payload script will loop for all the entries in flowVars.listOfMaps.
Following will work fine and map key value only once.
%dw 1.0
%output application/json
%var dataLookup = {(flowVars.listOfMaps map {
($.Id): $.Value
})}
---
payload map {
key : $.key,
data : dataLookup[$.key]
}
Output-
[
{
"key": "1234",
"data": "Text1"
},
{
"key": "1235",
"data": "Text2"
}
]
Where Payload -
[
{
"key" : "1234"
},
{
"key" : "1235"
}
]
And -
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
Hope this helps.
I create my own object with the slightly similar object and successfully access "value" field with the following DataWeave expression:
%dw 1.0
%output application/json
%var listOfMapsObject = flowVars.listOfMaps
---
payload map using(data = $) {
"key" : data.key,
"data" : (listOfMapsObject filter $.id == data.key).value reduce ($$ ++ $)
}
You can modify it with your own object, e.g.: replace the "id" with "Id". Test and evaluate the result by using filter, flatten, reduce, etc.

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.