Create json array using dataweave - mule

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

Related

Extracting a subset of JSON key-value pairs as an Object from a Parent JSON Object in dataweave 2.0 Mule 4

I have a dataweave challenge which I couldn't figure out without making it complicated.
Input JSON payload
{
"Name" : "Mr.wolf",
"Age" : 26,
"Region" : "USA",
"SubRegion": "Pacific"
}
Output needed
{
"Name" : "Mr.wolf",
"Age" : 26
}
Here is a catch. I am not allowed to use the above shown output format structure in the transform message, or either remove the keys using "-" operation.
So basically, we shouldn't use the below dwl.
%dw 2.0
output application/json
---
(payload - "Region" - "SubRegion")
or
%dw 2.0
output application/json
---
{
"Name" : payload.Name,
"Age" : payload.Age
}
How can we achieve the required output by using Lambdas, Reduce, mapObject, functions or any other operation of choice, other than the restricted methods/usage shown above.
Any solution provided is much appreciated.
is this what you are looking for?
%dw 2.0
output application/json
---
payload filterObject ((value, key,index) -> (index <2 ))
Sounds like filterObject could work for you. Documentation
payload filterObject ((value, key) -> (key ~= "Name" or key ~= "Age"))
Another rendition of the same approach.
%dw 2.0
output application/json
---
payload mapObject {
(($$) : $) if (($$) ~= "Name" or ($$) ~= "Age")
}
The other rendition being:
%dw 2.0
output application/json
---
payload mapObject {
(($$) : $) if (($$$) < 2)
}

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 accesing data with wildcard

Is it possible to access data with wildcard in dataweave?
I have my payload as Entry_1_m1,Entry_2_m1,Entry_3_m1
Entries will be dynamic there might be other entry as Entry_1_m2,Entry_2_m2,Entry_3_m2.
So I should be able to get the values irrespective of last number that is I should get the value of (Entry_1_m*). Is it possible in dataweave?
%dw 1.0
%input payload application/java
%output application/xml
---
{
Employee:{
empRequest:{
ReqNumber:payload.RequestNumber,
Reasons:{
Entry:payload.Entry_1_m1,
Entry:payload.Entry_2_m1,
Entry:payload.Entry_3_m1
}
}
}
}
Use pluck operator to get list of keys which will help you to map all fields starting with Entry_1_m. Following code worked for me
%dw 1.0
%input payload application/java
%output application/xml
%var keys = (payload pluck $$) filter ($ startsWith 'Entry_1_m')
---
{
Employee:{
empRequest:{
ReqNumber:payload.RequestNumber,
Reasons: {(keys map {
Entry:payload[$]
})}
}
}
}
Hope this helps.

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

Mule Transform Set Dynamic Key From Payload

I want to set up a key value dictionary from 2 different fields in my payload. My first thought is to use a transform component and dynamically set the key, but this code does not work:
%dw 1.0
%output application/java
---
{
payload.objectnumber: payload.objectid
}
I get this error:
Multiple markers at this line
- Invalid input "payload.", expected conditionalKeyValuePair,
keyValuePair, enclosedExpr or objectEnd
- no viable alternative at input 'payload'
- no viable alternative at input '.'
Any ideas on how to get it working?
Input:
[{
objectnumber: 75,
objectid: "abcdefgh"
}]
Expected Output:
[{
"abcdefgh": 75
}]
Put a variable inside bracket (value) to define a dynamic key. Therefore, you can try this code:
%dw 1.0
%output application/json
---
payload map {
($.objectid): $.objectnumber
}
Try the below code
<dw:transform-message metadata:id="b968b3ec-ed0a-492d-bf32-bcfe0d20e9d0" doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
%var objectId = payload.objectid
---
objectId :payload.objectnumber
]]></dw:set-payload>
</dw:transform-message>
JSON Input in Postman:
[
{
"objectnumber":75,
"objectid":"abcdefgh"
}
]