Dataweave Preparation Part - II | MuleSoft Guide

Extract only encoding and mediaType from below xml input

Input<?xml version='1.0' encoding='UTF-8'?><menusList>  <menus>    <menu>      <id>file</id>      <value>File</value>      <popup>        <menuitem>          <value>New</value>          <onclick>CreateNewDoc()</onclick>        </menuitem>        <menuitem>          <value>Open</value>          <onclick>OpenDoc()</onclick>        </menuitem>        <menuitem>          <value>Close</value>          <onclick>CloseDoc()</onclick>        </menuitem>      </popup>    </menu>  </menus></menusList>
Script%dw 2.0output application/json  ---{  "encoding": payload.^encoding,"mediaType": payload.^mediaType}
Output{  "encoding": "UTF-8",  "mediaType": "application/xml"}

Update the menusList value if the input payload type is object the print value as “this is an object” otherwise print payload in value section.

Input{  "menusList": {    "menus": {      "name": "sai"    }  }}
Script
%dw 2.0
output application/json
---
payload update
{
case mLit at .menusList if(payload is Object)-> "this is an object"
}
Output{  "menusList": "this is an object"}

Transform the below input into output format(exchange key with value and value with key) and each key value converted into single object, don’t use mapObject

Input{  "id": "file",  "value": "File",  "id": "file",  "value": "File"}
Script
%dw 2.0
output application/json
---
payload
pluck{
($):$$
}
Output[  {    "file": "id"  },  {    "File": "value"  },  {    "file": "id"  },  {    "File": "value"  }]

Convert json into csv format

Input[  {    "id": 123,    "name": "sample name",    "address": {      "pin": 534460,      "place": "ap",      "ph": 9848022338    }  },  {    "id": 1,    "name": "sample name2",    "address": {      "pin": 123,      "place": "ts",      "ph": 98765453459    }  }]
Script
%dw 2.0
output application/csv
---
payload
map(
$ - "address" ++ $.address
)
Outputid,name,pin,place,ph123,sample name,534460,ap,98480223381,sample name2,123,ts,98765453459

Find only duplicate values

Input
[1,2,3,4,5,6,7,1,2,7]
Script
%dw 2.0
output application/json
---
payload
filter(
sizeOf(payload find $)>1
) distinctBy($)
Output
[ 1,2,7]

Find only non-duplicate values

Input
[1,2,3,4,5,6,7,1,2,7]
Script
%dw 2.0
output application/json
---
payload
filter(
sizeOf(payload find $)<=1
) distinctBy($)
Output
[3,4,5,6]

Reformat below input into expected output

Input[  {    "pi1": "jawan",    "pi2": "",    "pi3": "",    "pi4": "wazir"  },  {    "pi1": "",    "pi2": "nawaz",    "pi3": "",    "pi4": "wazir"  },  {    "pi1": "",    "pi2": "",    "pi3": "",    "pi4": "wazir"  }]
Script
%dw 2.0
output application/json
---
payload
map ((item, index) -> (item orderBy -sizeOf($)) mapObject(
 ("pi" ++ $$$) : $
) )
Output[  {    "pi0": "jawan",    "pi1": "wazir",    "pi2": "",    "pi3": ""  },  {    "pi0": "nawaz",    "pi1": "wazir",    "pi2": "",    "pi3": ""  },  {    "pi0": "wazir",    "pi1": "",    "pi2": "",    "pi3": ""  }]

Reformat below input into expected output

Input{  "workTypeID": [    "08q4W000000xcHWQAY",    "08q4W000000xcHWQAY",    "08q4W000000xcHWQAY",    "08q4W000000xcHWQAY"  ],  "accountID": [    "0014W00002p2Ui0QAE",    "0014W00002ohJsWQAU",    "0014W00002oraWRQAY",    "0014W00002orO6qQAE"  ],  "recordTypeID": [    "0124W000001d2huQAA",    "0124W000001d2huQAA",    "0124W000001d2huQAA",    "0124W000001d2huQAA"  ]}
Script
%dw 2.0
output application/json

var itemCount = 1 to sizeOf((payload orderBy ((value, key) -> -sizeOf(value)))[0])
---
itemCount map({
workTypeID : payload.workTypeID[$$],
accountID : payload.accountID[$$],
 recordTypeID : payload.recordTypeID[$$]
})
Output[  {    "workTypeID": "08q4W000000xcHWQAY",    "accountID": "0014W00002p2Ui0QAE",    "recordTypeID": "0124W000001d2huQAA"  },  {    "workTypeID": "08q4W000000xcHWQAY",    "accountID": "0014W00002ohJsWQAU",    "recordTypeID": "0124W000001d2huQAA"  },  {    "workTypeID": "08q4W000000xcHWQAY",    "accountID": "0014W00002oraWRQAY",    "recordTypeID": "0124W000001d2huQAA"  },  {    "workTypeID": "08q4W000000xcHWQAY",    "accountID": "0014W00002orO6qQAE",    "recordTypeID": "0124W000001d2huQAA"  }]

Reformat below input into expected output

Input[  {    "student": "s1",    "maths": "pass",    "sciences": "fail",    "english": "pass"  },  {    "student": "s2",    "maths": "pass",    "sciences": "pass",    "english": "pass"  },  {    "student": "s3",    "maths": "fail",    "sciences": "fail",    "english": "pass"  }]
Script
%dw 2.0
output application/json
---
payload groupBy ($.student) pluck(
($$) : $ flatMap(valuesOf($ - "student")) filter($ ~= "pass")
)
Output[  {    "s1": [      "pass",      "pass"    ]  },  {    "s2": [      "pass",      "pass",      "pass"    ]  },  {    "s3": [      "pass"    ]  }]

Reformat below input into expected output

Input[  {    "sid": 123,    "sfname": "sai"  },  {    "sid": 123,    "slname": "kumar",    "snum": 9087654132  },  {    "sid": 124,    "sfname": "ravi",    "slname": "kanth"  },  {    "sid": 125,    "sfname": "hari"  }]
Script
%dw 2.0
output application/json
---
payload groupBy ($.sid) pluck({($)} distinctBy ($))
Output[  {    "sid": 123,    "sfname": "sai",    "slname": "kumar",    "snum": 9087654132  },  {    "sid": 124,    "sfname": "ravi",    "slname": "kanth"  },  {    "sid": 125,    "sfname": "hari"  }]
]]>
Post a Comment (0)
Previous Post Next Post