escape backslashes in dataweave - mule

I have a string array in properties file, and I want to read its value in dataweave in JSON format.
The array in properties file is-
Countries = ["USA","England","Australia"]
in dataweave, I am using this-
%output application/json
---
{
countries: p('Countries')
}
Output I am getting is-
"countries": "[\"USA\",\"England\",\"Australia\"]",
Output I want is-
"countries": [
"USA",
"England",
"Australia"
]
I have tried with replace but no luck.
I also tried countries map $ as String after changing country array to Countries = ['USA','England','Australia'] but it says Invalid input 'S', expected :type or enclosedExpr
How to achieve this?

The problem is that properties file values are strings and not arrays so your expression is not interpreted. But don't worry you can use the read function
read(p('Countries'), "application/json"))

Related

How to remove or trim special characters/prefixes from payload in Mule 4

I need to trim/remove the special characters from a payload.
example- input-
�~�{
"Employee": "Sara",
"number": {
"id": "d1q2",
"designation": "CYUL"
}
}
output-
{
"Employee": "Sara",
"number": {
"id": "d1q2",
"designation": "CYUL"
}
}
I am trying to remove the before characters of "Employee" before curly braces in the beginning whatever is present should be removed and just a JSON structure should be present starting from curly braces and before whatever comes should be removed tried everything unable to do. Please help me in resolving my issue.
You can't read the payload as JSON because those characters are not valid in JSON and the DataWeave parser will throw an error. If you can read the payload as text/plain then you can use DataWeave to remove the 4 extra characters from the beginning and re read the remaining data as a JSON, and set the output to JSON too.
%dw 2.0
output application/json
---
read(payload[4 to -1], "application/json")
If you already read the payload you could try setting its MIME type before executing the DataWeave Transform: <set-payload value="#[payload]" mimeTpy="text/plain"/>.

Mulesoft 4 Dataweave for loop and key value pair inside EXCEL TO JSON transformation

I have a data weave transformation converting an excel file to JSON, I had to change the value of an element (column of the file) as per the key-value pair values stored in a variable.
Please let me know how to achieve this.
Below is my data weave which converts 600 rows in the file to a JSON. However, I need to change the value for Brand as per the key-value pair mapping I stored in a variable.
%dw 2.0
output application/json
---
payload map(payload01,index01)->{
city: payload01.City,
province: payload01.Province,
phone: payload01.Phone,
fax: payload01.FAX,
email: payload01.EMAIL,
Brand: payload01.'Fuel Brand'
}
I understand that you want to use the value in attribute 'Fuel Brand' of the input payload to be used as the index in a variable:
%dw 2.0
output application/json
---
payload."Sheet Name" map(payload01,index01)-> {
city: payload01.City,
...
Brand: vars.brandsMapping[payload01.'Fuel Brand']
}
For example if the input is (note: I removed other attributes to simplify the example):
[{City=City A, Fuel Brand=brand1}, {City=City B, Fuel Brand=brand3}]
And the variable vars.brandsMapping contains:
{brand1=The brand1, brand2=The brand2, brand3=The brand3}
The output will be:
[
{
"city": "City A",
"Brand": "The brand1"
},
{
"city": "City B",
"Brand": "The brand3"
}
]
UPDATE: Since you clarified that you want to a dynamic mapping, the method that can be used for that is mentioned at the documentation page: https://docs.mulesoft.com/mule-runtime/4.1/dataweave-cookbook-map-based-on-an-external-definition

Filter Payload with Dataweave for a Set Variable component

For the Mulesoft 4.2 Set Variable component, I want to assign a simple String value pulled from a single specific record from an incoming JSON payload.
In my case, taking just the value from the 'country' field from below example:
[
{
"salesID": "4404Bob Builder210032011-02-18T15:52:21+0.00",
"id": "4404",
"firstName": "Bob",
"lastName": "Builder",
"address": "181 Construction Road, Adelaide, NSW",
"postal": "21003",
"country": "New Zealand",
"creationDate": "2011-02-18T15:52:21+0.00",
"accountType": "personal",
"miles": 17469
}
]
A non-dynamic way to do this is like:
payload[0].country
I believe the best way to do this is with the filter function. The below option gives me the entire object, but I just want the country field.
payload filter ($.id == "4404")
Map function seems to be overkill for this since I only want the value, itself. I must be missing the syntax to get at the country field.
I did some more investigating, and this solution got me close enough to what I wanted:
https://stackoverflow.com/a/43488566/11995149
For my code example using filter, I had to surround the whole expression in parenthesis, and then I can access the field with a dot reference,
but below code gives String value as a single record within an array:
(payload filter ($.id == "4404")).country
[
"New Zealand"
]
In my case, I know that just one result will be returned from the filtered payload, so I could get just the String value with:
(payload filter ($.id == "4404"))[0].country
enter image description here
Can you try any of below options:
1) (payload groupBy ((item, index) -> item.id))["4404"][0].country
OR
2) (payload map ((item, index) -> if(item.id == "4404") item.country else ""))[0]
Thanks,
Ashish

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.

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