Delete first and last quote mule 3 - mule

I try to delete the quotes around the payload that is incoming from http request, so I can use it as request for an another call (mule 3).
I use the next transformation syntax;
%dw 1.0
%output application/json
---
(payload.value.id) joinBy " or id eq "
and I get this as payload;
{
"#odata.context": "https://test.service.com/services/repo/v1/odata/$metadata#Incident",
"value": [
{
"id": "7001fc4a-8c8d-43f5-abc7-666666cbdd63",
"createDate": "2022-01-24T16:47:22Z"
},
{
"id": "ff8aeb42-210c-49d7-aa25-12da825b6b89",
"createDate": "2022-01-24T19:35:06Z"
}
]
}
I want to retrieve only the Id's and add id eq or see below.
how ever my desire output need to be like this;
9001fc4a-8c8d-43f5-abc7-146945cbdd63 id eq or
ff8aeb42-210c-49d7-aa25-19da825b6b89 id eq or
88eb39ee-d8f7-462c-9b69-1d4a1e3bf994 id eq or
66af209d-7635-4f51-95fd-204e2faea223 id eq or
5b93cf0d-e397-4cea-914b-3842c3aa0847 id eq or
I have tried with replace /["]/ with "" and with alot of connectors only I dont get it done.
In mule 4 it's just changing the to output text/plain and it removes al the quotes only mule 3 not.
Anyone experience with this.
Thanks for helping.

text/plain is reserved for fixed format in DataWeave for Mule 3. Try setting the output to application/java and the use it.

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"/>.

how properly to add and remove elements from payload? (or replace on condition)

So I have payload that have old type of attributes, and I want to migrate them to be as new ones that all the rest logic is using. So before do validation I want modify it a bit.
Currently I manage to add and remove in separate transforms, but should it be possible to do in one go?
example payload:
{
"country": "Country",
"town": "Town",
"district": "Dist",
"owner": "Owner"
}
and output should be:
{
"country": "Country",
"city": "Town",
"area": "Dist",
"owner": "Owner"
}
so I add transform:
%dw 1.0
%output application/json
---
payload ++ {city: payload.town}
when
payload.town != null
otherwise
payload ++ {area: payload.distrinct}
when
payload.distrinct != null
otherwise
payload
I want to check if payload have no null values in town key and add new key city with town key value, and same check if distrinct is not null then add its value as area key. However its happening only for city (I know it will be added at the bottom, but order is not a problem in my case) however keys may not present (it may no town, or may no distrinct or may no both)
And on next transform:
%dw 1.0
%output application/json
---
payload -- {town: payload.town}
when
payload.town != null
otherwise
payload
I try to check if keys exist then delete old ones, but no luck on such :(
Any help?
That's too complicated. Instead of adding and removing keys you can just use mapObject to transform each key. Then it becomes trivial to parametrize the transformation. Also using default is simpler than when...otherwise when a value is null.
%dw 1.0
%output application/json
%var keyMap={ town: "city", district: "area" }
%function replaceKey(keyName) (keyMap[keyName] default keyName)
---
payload mapObject ( (replaceKey($$)) : $ )

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 Dataweave : Filter in combination with Default function not working

Im using Mule dataweave, here is my request, i want to filter my request to code == "P" if request containing Code = p not present then always default to code == "C" because code = C always present in the incoming request.
Request:
{
"addresses": [
{
"contact": 0,
"code": "P",
"TypeDescription": "POSTAL",
"postcode": "1007",
"State": "TamilNadu",
"Description": "Test",
},
{
"contact": 1,
"code": "C",
"TypeDescription": "PHYSICAL",
"postcode": "Bangalore",
"State": "",
"Description": "NEW",
}
]
}
Dataweave:
%dw 1.0
%output application/json
---
payload.addresses filter $.code == "P"
It is working fine and filtering out the P containing list, but when try filter in combination with default it dont wont.
I have tried as below in dataweave payload.addresses filter $.code == "P1" default "anything here as of now".
Since in the above response i'm filtering P1 which is not present in the request hence im expecting the default stuff in the response. But it is not working.
Note: Using when and otherwise, bringing the list twice. I need only one list as response either Code P containing list or C containing list.
Using MUle 3.8.5 V. Please let me know your thoughts. Thanks in advance.
From what I understand, Dataweave works from right to left, so in this case is evaluating like this:
'"P1" default "anything here as of now"' resolves to "P1"
run 'filter $.code == "P1"' over 'payload.addresses'
Of course that first option will always evaluate to "P1" so the default is essentially ignored. You need to use parentheses around the rest of the expression, then follow it with default. Now it will evaluate the parentheses then the default expression.
eg: (payload.addresses filter $.code == "P1") default "anything here as of now"
This is not a good example though, as filter will return an empty list if none of the options match, not null, so the default won't work. You could try something like in this answer: https://stackoverflow.com/a/44431546/2614624
Create a default value in local variable and use an OR condition while filtering.
Example below.
{
biggerThanTwo: [0, 1, 2, 3, 4, 5] filter (($ > 2) OR ($ ==1))
}
Here is my Solution. As #Clinton mentioned Filter returns Empty array not null, here is the way it worked out
%dw 1.0
%output application/java
%var conditionalRoute = payload.addresses filter $.addressTypeCode == "P"
conditionalRoute when conditionalRoute != [] otherwise "anything here as of now"

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