in mule 4 transformer Is it possible to use above defined key below - anypoint-studio

I am working on mule 4. I want to use the key defined above in transformer below. for example, my transformer is
%dw 2.0
output application/json
---
{
name : usama,
age : 24,
value: age
}
here in the third key i.e. "data" I want to use the value of "age" key.
Can anyone help?
the output should be as defined below
{
name : usama,
age : 24,
value: 24
}

Hi you can either define a global variable and reference it in both places or use a do block
%dw 2.0
output application/json
var age = 24
---
{
name : "usama",
age : age,
value: age
}

Related

Extracting a subset of JSON key-value pairs as an Object from a Parent JSON Object in dataweave 2.0 Mule 4

I have a dataweave challenge which I couldn't figure out without making it complicated.
Input JSON payload
{
"Name" : "Mr.wolf",
"Age" : 26,
"Region" : "USA",
"SubRegion": "Pacific"
}
Output needed
{
"Name" : "Mr.wolf",
"Age" : 26
}
Here is a catch. I am not allowed to use the above shown output format structure in the transform message, or either remove the keys using "-" operation.
So basically, we shouldn't use the below dwl.
%dw 2.0
output application/json
---
(payload - "Region" - "SubRegion")
or
%dw 2.0
output application/json
---
{
"Name" : payload.Name,
"Age" : payload.Age
}
How can we achieve the required output by using Lambdas, Reduce, mapObject, functions or any other operation of choice, other than the restricted methods/usage shown above.
Any solution provided is much appreciated.
is this what you are looking for?
%dw 2.0
output application/json
---
payload filterObject ((value, key,index) -> (index <2 ))
Sounds like filterObject could work for you. Documentation
payload filterObject ((value, key) -> (key ~= "Name" or key ~= "Age"))
Another rendition of the same approach.
%dw 2.0
output application/json
---
payload mapObject {
(($$) : $) if (($$) ~= "Name" or ($$) ~= "Age")
}
The other rendition being:
%dw 2.0
output application/json
---
payload mapObject {
(($$) : $) if (($$$) < 2)
}

Problem with "when and otherwise" condition

I will let the code do the explanation.
Dataweave gives errors:
Unable to resolve reference of when
Unable to resolve reference of otherwise
Input Message: An array of objects. Though I have given only 1 object here.
[{
"Field1" : 12345,
"field2" : 10
}]
%dw 2.0
output application/json
---
payload map {
"test" : $.Field1 when $.field2 >= 1 otherwise ""
}
Nadeem there is no <expression> when <condition> otherwise <expression> in DW 2.0. Use if (condition) <then_expression> else <else_expression> instead.
So your code will be as follows:
%dw 2.0
output application/json
var data = [{
"Field1" : 12345,
"field2" : 10
}]
---
data map {
test : if ($.field2 >= 1) $.Field1 else ""
}

How to convert CSV to JSON in mule 4

Is there easy way to convert CSV into JSON in mule 4? Currently I'm doing it as below.
%dw 2.0
output application/json
---
(payload splitBy('\r\n')) map using( tmp = $ splitBy(',')) {
id : tmp[0],
name: tmp[1]
}
Try with following
%dw 2.0
output application/json
---
payload
Input :-
id,name
2,Tom
3,Jerry
And output produced is
[
{
"id": "2",
"name": "Tom"
},
{
"id": "3",
"name": "Jerry"
}
]
Hope this helps.
Best working solution, if you have a CSV with comma separated values and a first row with header is:
FIRST TRANSFORM MESSAGE
%dw 2.0
output application/csv headerLineNumber=0, header=true
---
payload
SECOND TRANSFORM MESSAGE
%dw 2.0
output application/dw
---
payload
Try following in Transform message
%dw 2.0
output application/json
payload map {
FirstName: $.FirstName,
LastName : $.LastName,
Department : $.Department,
Email : $.Email,
Phone : $.Phone,
CreatedDate : $.CreatedDate
}

mule : how to convert list of maps to list of JSon in dataweave?

I have input data as list of maps like [{id="200" name="aaa"},{id="100",name="shbd"}].
I want to transform it to JSON like below
{
[
{
id="200",
name="aaa"
},
{
id="100",
name="shbd"
}
]
}
If the fields(keys in map) do no change, then it is simple and straightforward. Now how to transform if I dont know the key values. For eg, what if after sometime the input of map is [{"age":90},{"age","45"}]
It's always better to do specific mapping but you can go with the following , it will transform it into JSON
%dw 1.0
%output application/json
---
payload
As Anirban said, please validate the json you want to transform.
You can use below transformation for o/p specified below:
Transformation
---------------
%dw 1.0
%output application/json
---
payload map {
"id" : $.id,
"name" : $.name
}
---------------
expected output
--------------
[
{
id="200",
name="aaa"
},
{
id="100",
name="shbd"
}
]

Break string into multiple flowvars in mulesoft

I have a String of 50 characters and I want to extract 3 smaller parts from it. I tried
%dw 1.0 %output application/json
---
payload map ((payload01 , indexOfPayload01) -> {
id: payload01.substring(0,2),
name: payload01.substring(2,14),
age: payload01.substring(14,16)
})
But that is not working. Any suggestions?
The "substring" operation on a string uses square brackets to denote position.
id: payload01[0..2]
Visit the Dataweave Operators doc for more information,
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#substring