DWL Interview Preparation Part - I | MuleSoft Guide

Merge all the array of objects into single object

Input[ { "id": 123, "name": "sample name" }, { "message": "Hello world!" }]
Script
%dw 2.0
output application/json
---
{
(payload)
}
Output
{
"id": 123,
"name": "sample name",
"message": "Hello world!"
}

Extract only id and name from the given input without using one to one mapping.

Input
[
{
"id": 123,
"name": "sample name",
"message": "Hello world!"
}
]
Script
%dw 2.0
output application/json
---
(payload map ($ - "message"))[0]
Output{"id": 123,"name": "sample name"}

If any values from input array present in this array [1,3,4,5] then only return those values from input array.

Input
[ 1, 2, 3, 4 ]
Script%dw 2.0output application/jsonimport * from dw::core::Arraysvar tmpArray :Array = [1,3,4,5]---payload -- (payload -- tmpArray)
Output[ 1, 3, 4 ]

Find the days between two dates

Input
["2022-10-01T23:57:59-03:00", "2022-11-01T23:57:59-03:00"]
Script%dw 2.0output application/json---daysBetween(payload[0],payload[1])
Output31

Extract the names whose name end with “i”

Input["vamsi","hari","ravi","sandy","kumar"]
Script
%dw 2.0
output application/json
---
payload filter ((item, index) -> item endsWith  "i")
Output["vamsi","hari","ravi"]

Extract only upper-case key value pairs

Input
{"A": 1324,"b": 1432,"v": 9237,"D": 24365,"R": 213254}
Script
%dw 2.0
output application/json
---
payload filterObject ((value, key, index) -> upper(key) ~= key)
Output
{"A": 1324,"D": 24365,"R": 213254}

Extract the index if the Number value is 123 for any key

Input
{"id": 123,"sampleid": "123","id": 123,"name": "sample name"}
Script
%dw 2.0
output application/json  skipNullOn="everywhere"
---
payload pluck (if ($ == 123) $$$ else null)
Output
{"A": 1324,"D": 24365,"R": 213254}

Extract only float(decimal) values from input

Input
[[3,5],[0.9,5.5]]
Script
%dw 2.0
output application/json
---
flatten(payload) filter( isDecimal($))
Output
[0.9,5.5]

Extract only keys from object

Input
{"id": 123,"sampleid": 123,"id": 123,"name": "sample name"}
Script
%dw 2.0
output application/json
---
keysOf(payload)
Output
["id","sampleid","id","name"]

Convert below json to xml

Input[  {    "menu": {      "id": "file",      "value": "File",      "popup": {        "menuitem": [          {            "value": "New",            "onclick": "CreateNewDoc()"          },          {            "value": "Open",            "onclick": "OpenDoc()"          },          {            "value": "Close",            "onclick": "CloseDoc()"          }        ]      }    }  }]
Script
%dw 2.0
output application/xml
---
menusList : menus: {(payload)}
Output<?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>
]]>
Post a Comment (0)
Previous Post Next Post