Assign Array to String in mule - mule

How to assign array to String? I want to convert XML to CSV. My dataweave returning array. I want to convert it into CSV.
Transform Message Code:
%input payload application/xml
%output application/java
---
(payload.catalog.*category-assignment default []) groupBy $.#product-id pluck {
product-id:$$,
cat-id: $.#category-id joinBy ":",
primary-flag:$.primary-flag,
field3:$.#category-id when $.primary-flag[0] == "true" otherwise ""
}
My payload is like:
[{
product - id = D198561, cat - id = 1111, primary - flag = null, field3 =
}, {
product - id = D198563, cat - id = 30033, primary - flag = [true], field3 = [30033]
}, {
product - id = D198566, cat - id = 0933: 2104: 7043, primary - flag = null, field3 =
}]
I want output in CSV with respect to payload as:
Field1: product-id, Field2:cat-id,Field3:field3
Its giving error in field 3 as it is array like field3=[30033].
Thanks in advance

Related

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

Extract field from array using DW 1.0 function

I have a session variable called varIP1 which has a value like below: (It's mime type is application/java)
{abc={FedId=abc, Id=01FcLA, type=User, Profile={Id=02EmQA, type=Profile, Name=Analyst}}}
I am interested in extracting the first Id (01FcLA) from above using a function as given below:
%dw 1.0
%output application/json
%var myLib = readUrl("classpath://dwlib/my-global-functions.wev", "application/dw")
---
{
"Id": (myLib.idLookup(sessionVars.varIP1 ,$.Id,"Id") default null)
}
The global function that I'm using are:
%function validateLookup(lookupArray, key) 'true' when (lookupArray != null and IsNull(key) == 'false' and lookupArray[key] != null) otherwise 'false'
%function idLookup(lookupArray,key,value) (lookupArray[key][0][value] default '') as :string when (validateLookup(lookupArray,key) == 'true') otherwise null
With the above code, "Id" is coming as null. Any modifications needed above?
Thanks.
This would be the simplest approach if you know that your sessionVar will have the same structure:
%dw 1.0
%output application/json
---
{
Id: sessionVars.varIP1..Id
}
Updated, since abc can't be hardcoded.
There's no reason to put default null. If it can't find the Id, it will be null. If you want to default to "", that would make a little more sense.

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.

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