Dataweave Preparation Part - III | MuleSoft Guide

Reformat below input(it should be text format) into expected output

NOTE:

  1. extract only sid=123 data
  2. input type should be text not json
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 filter ($.sid ~= 123))
}
Output{  "sid": 123,  "sfname": "sai",  "sid": 123,  "slname": "kumar",  "snum": 9087654132}

Reformat below input(it should be text format) into expected output

Hint: which symbol we use when we ask a question. 

Input
Did not find your language?Propose yourself to translate our contents by sending an email with your references and your suggestion.What are the origins of Lorem Ipsum?Contrary to what one might think, the Lorem ipsum text, despite being meaningless, has noble origins.
Script
%dw 2.0
output application/json
---
payload splitBy  "." map{
 question : ($ splitBy  "?")[0],
answer : ($ splitBy  "?")[1]
}
Output[  {    "question": "Did not find your language",    "answer": "Propose yourself to translate our contents by sending an email with your references and your suggestion"  },  {    "question": "What are the origins of Lorem Ipsum",    "answer": "Contrary to what one might think, the Lorem ipsum text, despite being meaningless, has noble origins"  }]

Implement a script that can reformat below input into expected output

note-1:- the root tag(Divide) name should be dynamic and that value will be pass through a operation variable 

in this case operation=”Divide”.

note-2:- the parent tag names also dynamic

note-3:- name Space should be <strong>http://tempuri.org/</strong>

Input{  "intA": "40",  "intB": "50",  "intC": "12"}
Script
%dw 2.0
output application/xml

var operation="Divide"
var namespace = {uri: "http://tempuri.org/", prefix: "ns0"} as Namespace
---
namespace#"$(operation)" : payload mapObject{
 namespace#"$($$)" : $
}
Output<?xml version='1.0' encoding='UTF-8'?><ns0:Divide xmlns:ns0="http://tempuri.org/">  <ns0:intA>40</ns0:intA>  <ns0:intB>50</ns0:intB>  <ns0:intC>12</ns0:intC></ns0:Divide>

Implement a script that can reformat below input into expected output

************************( HARD )*******************************

Note: This problem is very challenging. Share your solution with us if you can find one. We will give you our solution and your details as well.

Input
92345324
Script%dw 2.0import * from dw::core::Stringsvar oneToNine = {  "1": "one",  "2": "two",  "3": "three",  "4": "four",  "5": "five",  "6": "six",  "7": "seven",  "8": "eight",  "9": "nine"}var tenTotwenty = {  "1": "eleven",  "2": "twelve",  "3": "thirteen",  "4": "fourteen",  "5": "fifteen",  "6": "sixteen",  "7": "seventeen",  "8": "eighteen",  "9": "nineteen"}var secondposetion = {  "2": "twenty",  "3": "thirty",  "4": "forty",  "5": "fifty",  "6": "sixty",  "7": "seventy",  "8": "eighty",  "9": "ninty"}var remaining = {  "3": "hundred",  "4": "thousand",  "5": "lakh",  "8": "crore"}fun convertToSChar(num: Number) = (do {  var tmpData = []  var size = sizeOf(num)  var single = if (size == 1)    tmpData << oneToNine[num[-1]]  else    []  var ten = flatten(tmpData << (if (num[-2] ~= 1)    tenTotwenty[num[-1]]  else    (tmpData << secondposetion["$(num[-2])"] << oneToNine[num[-1]]))) default []  var hundred = (tmpData << oneToNine[num[-3]] << remaining["$(sizeOf(num[-3 to -1]))"]) default []  var thousand = (tmpData << oneToNine[num[-4]] << remaining["$(sizeOf(num[-4 to -1]))"]) default []  var thousands = flatten(tmpData << (if (num[-5] ~= 1)    tenTotwenty[num[-4]]  else    (secondposetion["$(num[-5])"]))) default []  var lack = (tmpData << oneToNine[num[-6]] << remaining["$(sizeOf(num[-5 to -1]))"]) default []  var lacks = flatten(tmpData << (if (num[-7] ~= 1)    tenTotwenty[num[-6]]  else    (secondposetion["$(num[-7])"]))) default []  var crore = flatten(tmpData << tmpData << oneToNine[num[-8]] << remaining["$(sizeOf(num[-8 to -1]))"]) default []  ---  flatten([    crore,    lacks,    lack,    thousands,    thousand,    hundred,    ten,    single  ])})output application/json  ---if (payload == 0)  "zero"else if (isEmpty(payload))  "please pass the number"else  convertToSChar(payload) joinBy " "
Output
"nine crore twenty three lakh forty five thousand three hundred twenty four"

Implement a script that can sum all the numbers from below input 

NOTE: don’t use sum() implement Recursion function

Input
[1,2,3,4,5,6,7,8,9,10]
Script
%dw 2.0
output application/
json
---
sum(payload)
Output
55

Reformat below input into expected output

NOTE: The start and end values are dynamic, and change based on the input values. The respective values also depend on the values, position, and some mathematical operations.

Input[  {    "ID": 1234,    "Name": "salary Account"  },  {    "Name": "personal",    "ID": 987  },  {    "Name": "Loan Account",    "ID": 654  }]
Script
%dw 2.0
output application/
json
---
payload map (
 $ ++ {
Start : sizeOf($.ID),
End : sizeOf($.Name) - sizeOf($.ID) + $$
})
Output[  {    "ID": 1234,    "Name": "salary Account",    "Start": 4,    "End": 10  },  {    "ID": 987,    "Name": "personal",    "Start": 3,    "End": 6  },  {    "ID": 654,    "Name": "Loan Account",    "Start": 3,    "End": 11  }]

Reformat below input into expected output

NOTE: The start and end values are dynamic, and change based on the input values. The respective values also depend on the values, position, and some mathematical operations.

It’s similar to above question. 

Input

[
{
"ID": 1234,
"Name": "salary Account"
},
{
"Name": "personal",
"ID": 987
},
{
"Name": "Loan Account",
"ID": 654
}
]

Script%dw 2.0output application/json---payload map ($ ++ (if(($$ mod 2) == 0){Start : sizeOf($.ID),End : $.ID/2 - (sizeOf($.Name) + sizeOf($.ID) + $$ )}else{start: sizeOf($.ID),end: $.ID/2 + (sizeOf($.Name) + sizeOf($.ID)) + $$}))
Output[  {    "ID": 1234,    "Name": "salary Account",    "Start": 4,    "End": 599  },  {    "Name": "personal",    "ID": 987,    "start": 3,    "end": 505.5  },  {    "ID": 654,    "Name": "Loan Account",    "Start": 3,    "End": 310  }]

Reformat below input into expected output

Input<?xml version='1.0' encoding='UTF-8'?><catalog>  <book id="bk101">    <author>Gambardella, Matthew</author>    <title>XML Developer's Guide</title>    <genre>Computer</genre>    <price>44.95</price>    <publish_date>2000-10-01</publish_date>    <description>An in-depth look at creating applicationswith XML.</description>  </book>  <book id="bk112">    <author>Galos, Mike</author>    <title>Visual Studio 7: A Comprehensive Guide</title>    <genre>Computer</genre>    <price>49.95</price>    <publish_date>2001-04-16</publish_date>    <description>Microsoft Visual Studio 7 is explored in depth,looking at how Visual Basic, Visual C++, C#, and ASP+ areintegrated into a comprehensive developmentenvironment.</description>  </book></catalog>
Script
%dw 2.0
output
application/json writeAttributes=true
---
payload
Output{  "catalog": {    "book": {      "@id": "bk101",      "author": "Gambardella, Matthew",      "title": "XML Developer's Guide",      "genre": "Computer",      "price": "44.95",      "publish_date": "2000-10-01",      "description": "An in-depth look at creating applications \nwith XML."    },    "book": {      "@id": "bk112",      "author": "Galos, Mike",      "title": "Visual Studio 7: A Comprehensive Guide",      "genre": "Computer",      "price": "49.95",      "publish_date": "2001-04-16",      "description": "Microsoft Visual Studio 7 is explored in depth,\nlooking at how Visual Basic, Visual C++, C#, and ASP+ are \nintegrated into a comprehensive development \nenvironment."    }  }}

Reformat below input into expected output

Note: based on input value, key will be changed in output. 

Input{  "Tablelist": [    {      "Name": "nemalidinne_satish_reddy"    },    {      "Name": "nemalidinne_ANIL_reddk"    },    {      "Name": "Madhinnene_SAI_Chowdery"    },    {      "Name": "Vadlamudi_Singhaia_Chowdarr"    },    {      "Name": "Vadlamudi_vamsi_Chowdary"    }  ]}
Script
%dw 2.0
output
application/json writeAttributes=true
---
{
(payload.Tablelist.Name map (
($[0] ++ $[-1]): $
))
}
Output{  "ny": "nemalidinne_satish_reddy",  "nk": "nemalidinne_ANIL_reddk",  "My": "Madhinnene_SAI_Chowdery",  "Vr": "Vadlamudi_Singhaia_Chowdarr",  "Vy": "Vadlamudi_vamsi_Chowdary"}

Reformat below input into expected output

note:- maintain the output order also

Hint: short the data. 

Input[  {    "product name": "bread",    "ExpairyDate": "11/01/2011",    "Price": 40  },  {    "product name": "Apple",    "ExpairyDate": "22/12/2012",    "Price": 60  },  {    "product name": "milk",    "ExpairyDate": "12/11/2023",    "Price": 60  },  {    "product name": "bread",    "ExpairyDate": "04/05/2023",    "Price": 50  },  {    "product name": "bread",    "ExpairyDate": "03/05/2023",    "Price": 49  },  {    "product name": "milk",    "ExpairyDate": "12/11/2039",    "Price": 140  }]
Script
%dw 2.0
output
application/json writeAttributes=true
---
valuesOf(payload groupBy $."product name") orderBy(- sizeOf($)) flatMap(
  $ map(
$ ++ {
"item" : $$ + 1
}
))
Output[  {    "product name": "bread",    "item": 1,    "ExpairyDate": "11/01/2011",    "Price": 40  },  {    "product name": "bread",    "item": 2,    "ExpairyDate": "04/05/2023",    "Price": 50  },  {    "product name": "bread",    "item": 3,    "ExpairyDate": "03/05/2023",    "Price": 49  },  {    "product name": "milk",    "item": 1,    "ExpairyDate": "12/11/2023",    "Price": 60  },  {    "product name": "milk",    "item": 2,    "ExpairyDate": "12/11/2039",    "Price": 140  },  {    "product name": "Apple",    "item": 1,    "ExpairyDate": "22/12/2012",    "Price": 60  }]
]]>
Post a Comment (0)
Previous Post Next Post