Problem with "when and otherwise" condition - mule

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

Related

Splitting concatenated values in table column using Dataweave

I have sample data in my Salesforce table as given below:
I am on Mule 3.9 and running dataweave 1.0.
I need to use dataweave to read the above data (from a Salesforce table) and transform it into a JSON as given below:
[
{"Id": "634594cc","Name": "Alpha","List": "AB01"},
{"Id": "634594cc","Name": "Alpha","List": "AB02"},
{"Id": "634594cc","Name": "Alpha","List": "AB03"},
{"Id": "5d839e9c","Name": "Bravo","List": "CD01"},
{"Id": "5d839e9c","Name": "Bravo","List": "CD02"},
{"Id": "3a5f34d3","Name": "Charlie","List": null}
]
As you can see above, the "List" column is what I need to split as separate arrays in the final JSON. It has data with semicolon at the begin, in between and in the end.
Thanks in advance for your help.
I took the liberty to create a couple of sample data based upon the SS (BTW, its best not to use SS) :).
Try this:
%dw 1.0
%output application/dw
%var data = [
{
id: "ABC123",
name: "A",
list: ";AB1;AB2;AB3;"
},
{
id: "ZXY321",
name: "B",
list: null
}
]
---
data reduce (e,result=[]) -> (
result ++ using (
list = e.list default "" splitBy /;/ filter ($ != ""),
sharedFields = {
Id: e.id,
Name: e.name
}
) (
using (
flist = list when ((sizeOf list) > 0) otherwise [null]
) (
flist map {
(sharedFields),
List: $
}
)
)
)

how to get string values from list of maps in dataweave 2.0?

I have input payload coming like this -
[
{
"a": ""
},
{
"a": "abc"
},
{
"a": "pqr"
},
{
"a": "xyz"
}
]
and desired output is abc,pqr,xyz
I tried following dwl but couldn't succeed. Here is the code snippet
%dw 2.0
output application/json
query : payload filter ($.a != '') map (
$.a
)
Can someone please help me with the dataweave ? Thanks.
If your desired output is the string "abc,pqr,xyz":
%dw 2.0
output application/json
---
payload filter ($.a != "") map ($.a) joinBy ","
If you are trying to get the array ["abc", "pqr", "xyz"]:
Your code is fine...
%dw 2.0
output application/json
---
payload filter ($.a != "") map ($.a)
query: joinBy(payload.a filter $ !="", ',')
First select all 'a' fields to return new array of just values.
Filter the list for "".
Use joinBy function to append array values with a comma.

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
}

Mulesoft Dataweave Converting the key of a mapObject to lower case

Assume the following json with KEY1 and KEY2 in caps. KEY1 and KEY2 needs to be converted to lower case
{
"KEY1": {
"subkey1": "subval1",
"subkey2": "subval2"
},
"KEY2": {
"subkey1": "subval1",
"subkey2": "subval2"
}
}
this needs to be converted to the following json using data weave.
{
"key1": {
"subkey1": "subval1",
"subkey2": "subval2"
},
"key2": {
"subkey1": "subval1",
"subkey2": "subval2"
}
}
I tried the following DW syntax, but it did not work
result : payload mapObject (
lower '$$':$
)
The DW you tried should work if you wrap the expression in parenthesis. This ensures that the lower operator is applied to each of the keys first and then that value is used in the map. So for your example:
%dw 1.0
%output application/json
---
{
result : payload mapObject (
(lower '$$') : $
)
}
Interestingly enough, I get an error (mismatched input ':' expecting ')') in my Transform Message using this DW syntax but I am able to run the project without complaints from Anypoint Studio and the DW runs fine. It also works in MEL with the following:
#[dw("{result : payload mapObject ( (lower '$$' ) : $)}", 'application/json')]
Hope that helps!
You can use the below method to resolve the issue.
%dw 2.0
output application/json
---
{ result: payload mapObject (
(lower ('$$')): $
)
}
In addition if need to lower case also a values then its will be like:
%dw 1.0
%output application/json
---
{
result : payload mapObject (
(lower $$) : (lower $)
)
}

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