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
€67.18
List Price:  €95.97
You save:  €28.79
£55.86
List Price:  £79.80
You save:  £23.94
CA$97.87
List Price:  CA$139.82
You save:  CA$41.95
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,267.89
List Price:  R1,811.35
You save:  R543.45
Лв131.37
List Price:  Лв187.69
You save:  Лв56.31
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪259.10
List Price:  ₪370.16
You save:  ₪111.06
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$69,057.73
List Price:  CLP$98,658.13
You save:  CLP$29,600.40
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,776.20
List Price:  ISK kr13,966.60
You save:  ISK kr4,190.40
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,259.44
List Price:  ARS$100,374.93
You save:  ARS$30,115.49
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
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.