How to throw an error message if id field exist in POST request in Mulesoft 4 - anypoint-studio

How to check if id in post request should not be included.
I have 2 scenarios.
request body:
{
"name"="John"
}
Expected result:
"Success"
request body:
{
"id"="323",
"name"="Jane"
}
Expected result:
"id field should not be specified."

The following code will help you with what you are looking for
%dw 1.0
%output application/json
---
{
output: 'ID field should not be specified'
when payload.id? otherwise 'Success'
}
in mule 4: when otherwise has been replaced with if else.
here is the dataweave code
%dw 2.0
output application/json
---
if (payload.id?)
{ result: "ID field should not be specified" }
else { result: "Success" }

Related

Delete first and last quote mule 3

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.

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

Create json array using dataweave

If I have xml like so....
<Root>
<Authority>Water</Authority>
<Sanctions>
<Sanction>
<SanctionCode>11</SanctionCode>
<SanctionDesc>First Sanction</SanctionDesc>
</Sanction>
<Sanction>
<SanctionCode>11</SanctionCode>
<SanctionDesc>Second Sanction</SanctionDesc>
</Sanction>
</Sanctions>
</Root>
Using DataWeave how can I create a json array of Santions using only the SanctionDesc?
I've tried this but it's not right...
%dw 1.0
%output application/json
---
records: payload.Root map {
Authority: $.Authority,
sanctions: $.Sanctions.Sanction map [$.SanctionDesc]
}
I want my output to look like this...
{
"records": [{
"Authority": "Water",
"sanctions": ["First Sanction", "Second Sanction"]
}]
}
Try this
%dw 1.0
%output application/json
---
records: {
Authority: payload.Root.Authority,
sanctions: payload.Root.Sanctions..SanctionDesc
}
Or
%dw 1.0
%output application/json
---
records: {
Authority: payload.Root.Authority,
sanctions: payload.Root.Sanctions.*Sanction map $.SanctionDesc
}
Hope this helps.
Understading the map operator is the key when picking and choosing elements from input payload. Map operator goes through all the array elements on the left hand side and we can pick the values from on right hand side, Giving example from Mulesoft portal
%dw 1.0
%output application/json
users: ["john", "peter", "matt"] map ((firstName, position) -> position ++ ":" ++ upper firstName)
Output:
{
"users": [
"0:JOHN",
"1:PETER",
"2:MATT"
]
}
See link below:
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#map