Writing Complex transformations-part2

Mule 4 Indepth DataWeave2 Transformations
16 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.14
List Price:  €93.07
You save:  €27.92
£55.73
List Price:  £79.62
You save:  £23.88
CA$95.61
List Price:  CA$136.60
You save:  CA$40.98
A$106.30
List Price:  A$151.87
You save:  A$45.56
S$94.64
List Price:  S$135.20
You save:  S$40.56
HK$546.91
List Price:  HK$781.33
You save:  HK$234.42
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr764.69
List Price:  NOK kr1,092.46
You save:  NOK kr327.77
DKK kr485.92
List Price:  DKK kr694.20
You save:  DKK kr208.28
NZ$117
List Price:  NZ$167.15
You save:  NZ$50.15
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,661.98
List Price:  ৳10,946.16
You save:  ৳3,284.17
₹5,839.65
List Price:  ₹8,342.71
You save:  ₹2,503.06
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,575.56
List Price:  ฿3,679.53
You save:  ฿1,103.97
₺2,262.43
List Price:  ₺3,232.18
You save:  ₺969.75
B$357.76
List Price:  B$511.10
You save:  B$153.34
R1,296.01
List Price:  R1,851.52
You save:  R555.51
Лв127.38
List Price:  Лв181.98
You save:  Лв54.60
₩95,113.23
List Price:  ₩135,881.87
You save:  ₩40,768.63
₪260.11
List Price:  ₪371.60
You save:  ₪111.49
₱3,999.61
List Price:  ₱5,713.97
You save:  ₱1,714.36
¥10,715.43
List Price:  ¥15,308.41
You save:  ¥4,592.98
MX$1,185.45
List Price:  MX$1,693.57
You save:  MX$508.12
QR254.79
List Price:  QR364.01
You save:  QR109.21
P955.69
List Price:  P1,365.33
You save:  P409.64
KSh9,427.65
List Price:  KSh13,468.65
You save:  KSh4,041
E£3,355.67
List Price:  E£4,794.02
You save:  E£1,438.35
ብር3,989.43
List Price:  ብር5,699.43
You save:  ብር1,710
Kz58,616.62
List Price:  Kz83,741.62
You save:  Kz25,125
CLP$66,326.02
List Price:  CLP$94,755.52
You save:  CLP$28,429.50
CN¥506.51
List Price:  CN¥723.62
You save:  CN¥217.11
RD$4,049.59
List Price:  RD$5,785.38
You save:  RD$1,735.78
DA9,420.19
List Price:  DA13,457.99
You save:  DA4,037.80
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.62
List Price:  Q775.21
You save:  Q232.58
GY$14,613.08
List Price:  GY$20,876.73
You save:  GY$6,263.64
ISK kr9,792.30
List Price:  ISK kr13,989.60
You save:  ISK kr4,197.30
DH706.05
List Price:  DH1,008.69
You save:  DH302.63
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,010.92
List Price:  ден5,730.13
You save:  ден1,719.21
MOP$562.15
List Price:  MOP$803.11
You save:  MOP$240.95
N$1,302.54
List Price:  N$1,860.85
You save:  N$558.31
C$2,571.43
List Price:  C$3,673.63
You save:  C$1,102.20
रु9,317.58
List Price:  रु13,311.40
You save:  रु3,993.82
S/262.81
List Price:  S/375.46
You save:  S/112.65
K268.53
List Price:  K383.63
You save:  K115.10
SAR262.51
List Price:  SAR375.03
You save:  SAR112.52
ZK1,879.71
List Price:  ZK2,685.42
You save:  ZK805.70
L324.19
List Price:  L463.14
You save:  L138.95
Kč1,629.65
List Price:  Kč2,328.17
You save:  Kč698.52
Ft25,373.17
List Price:  Ft36,248.95
You save:  Ft10,875.77
SEK kr758.75
List Price:  SEK kr1,083.98
You save:  SEK kr325.22
ARS$61,468.94
List Price:  ARS$87,816.53
You save:  ARS$26,347.59
Bs482.36
List Price:  Bs689.12
You save:  Bs206.75
COP$272,946.91
List Price:  COP$389,940.87
You save:  COP$116,993.96
₡35,623.88
List Price:  ₡50,893.45
You save:  ₡15,269.56
L1,732.95
List Price:  L2,475.75
You save:  L742.80
₲523,151.84
List Price:  ₲747,391.81
You save:  ₲224,239.96
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł281.85
List Price:  zł402.67
You save:  zł120.81
Already have an account? Log In

Transcript

Welcome back. So in this video also will see some complex transformations tempesta. My first requirement again here is the here is offer price, I want to actually do the offer price based on the country's currency. I want to convert the value based on the conversion price. So right now simply what I'll do is I'll create a variable conversion rate. I will initialize it with 60 or 70, we'll say and then how to use this variable.

Can I use variable conversion rate inside my expressions? Yes, I should be able to do it. So maybe I want to multiply this offer price with conversion rate. Yep. I can see that it is multiplies. Yes.

So now you understood how to use a variable. How to actually initialize a variable with a constant Like integer number and how to use that variable inside expressions like by multiply how to write a function I want this converted price to return by a function. How to write a function. It's very simple we have to use fun. Actually if you are if you have already used v w one dot zero dw version, it was function and in dw two dot zero we have named as fun not function per se get converter rights and let my function argument also my price is equal to I want my price to be multiplied using conversion rate. Okay, I have a function how to use it.

Let me give you the function name, get converted price and pass argument, product dot offer, as you offer offer. So what same outputs? Yes. So I defined a function, which is returning the converted price and using the function in expression. That's great. Now, if you're familiar with JavaScript syntaxes, we know in JavaScript, we can initialize a variable pointing to an anonymous function.

I also want to do the same thing here. I want to define a variable pointing to anonymous function that electrophilic convert is equals to, I want to define an anonymous function so all you hear my price is an argument to the function there's no function name, whenever you're using animals functional is syntax like this lambda, an arrow like this. And I want the function to return this is same. So this is a variable, sorry, this is a variable pointing to a function, which does not have a name. How to use this variable, just like I have used the function, so I can directly use here, instead of using this get converted price, I can simply say okay, Okay. Now I also wanted to display images for each product.

Actually, if we see the input, input fail or separate pill or every product is having a list of images, I want to iterate over each image here. And for each image I want to emit IMG pack. Let's see. So I wanted to read or images. So here what I'll do is I said he may just IMG is a column, fill out a dot image is actually an array, not payload it is product or sorry, the current element is product. Very careful when using it.

So yeah, I actually wrote that down. He means See, I got three IMG stacks because the value is an array or list for each element I got one IMG no that's not what I want. I want one img tag per image rapidly ranges very quick. So, first of all I want the root tag to be I emgs. As you know already if you want tag name to be images he says I want it right and I want to emit one IMG. So, perfect for recording his lap lap operatory.

Now, this map operator will emit a list right. You know how to flatten a list by using current Because right now there is no error, but for each element I want to emit img tag colon dollar the current element. See, yes. Now it looks like I got one img tag for each image wrapper inside IMG yes is that this looks very complex expressions becoming complex, always better idea is to split one complex expression into small small functions. So what I'll do is I'll write a function fun, get images, analytic image, email, this is it. What would I have You know Ray Have you take all this content and then I'll simply call the function get images off or try to pass product images and here I should use simply made a document yes now I got the same out is it always this no no this function is this function is very clear if I give The majors are just giving IMG tags, right?

Is it reading or images. Now, I'm calling this function now, since I split one complex expression into small small functions, this looks readable is the best practice, please follow it. Okay, now coming back here every product is having images. Let's say there's one product without images. I'll remove images first products. first product is not having any images.

So let's see here is our first product. There is nothing I even don't want i emgs tag also. See here this is irrelevant to empty but I don't even I emgs tacos So I want this img tag to present only when there are images. So I want this img tag to appear conditionally. I want to add a condition here to display this whole key value pair. I'll wrap this inside parenthesis, I will try to write a condition.

I will try try to when actually, when is the key word which is available in Data View version one dot zero. But now to LOD zero, there is no operator, there is no when is up that I have used if if product.me Yes, question mark. Question mark is an existing opera existence opportunity check the email yesterday. present or not? If it's present question mark operator returns true else false. So basically here I'm emitting a emgs tag.

See, there's no mg star here, right? Basically, I'm emitting IMG stack based on the presence of images in the input. So for the first product, you can see there is no i images at all. So, the condition is not satisfied. There is no IMG is it? So that's how to use if conditional operator.

Okay, so let's find right now. And one more thing is here, I have actually hard coded the conversion rate. I don't want to hard code the conversion rate. If you see my actual XML I have one more flow in this XML. This flow is named as get conversion rate flow. Ricky's having choice.

So, this the control will go to the first first route only when payload is us second route when failed us UK else it will go to third one right. So, based on the payload, the the flow will either execute this one or this one or this one. So, the first route, if you see it is setting the payload as the first route is setting the payload as 65 second one is setting the payload as 75 and third one as one. So, basically if I can call this flow probably passing some payload, this flow will return either 65 or 75 or one is it So, Causes flow from my dw the flow name I will try to get how to get this how to call this. So, if you see the DW how to call it flow if you know a name here's of hard coding or elbows there's a function called let's look up.

First argument for the lookup should be the favorite what I should pass I'll hardcourt as us. Us second argument should we what is the payload? Sorry. Sorry, the first argument is a flow name which you want to look up. And second argument is a payload What are you want to pass the flow? I pass hardcore as you pass.

Yes, this is a way to call any function for anything flow within our data, so, we are configured is to call get conversion rate flow passing the payload as us what are the flow is returning with that the variable is initialized Great. Now Will I be able to see the preview and maximize it may be able to see the preview not on issue form because this lookup has to execute the flow right fluoride you cannot see the preview whenever you're calling look up. And one more thing is I don't want to have hardcore us I want it to be dynamic. So pass attributes got query parents got left the country bears in the country, the conversion rate We'll be decided, Okay, that looks good. Let me run this now. Okay deployed.

So let me give a request or torque request to slash transform. And I need to pass country as a query parameters it slash transform question mark. country is across to us. I didn't actually make a POST request with products. Jason. This is expected payload sent.

Oh, actually, I should pass it on as us. Actually pause us and then I can make a request. Yep, now you have seen the value multiplied by 65. I'll pause UK. Now, offer price is paid out loud we got it multiplied by 75 and if it was anything like I am India see now you can see that the offer price is multiplied by one, I'm able to get the response as expected that great now you understand less example how to actually look up Flow also by using

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.