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.
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
}))
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: $$ }
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
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
}
]
}
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
}