Writing Complex transformations-Part1

Mule 4 Indepth DataWeave2 Transformations
27 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.35
List Price:  €96.21
You save:  €28.86
£55.83
List Price:  £79.77
You save:  £23.93
CA$100.76
List Price:  CA$143.95
You save:  CA$43.19
A$112.23
List Price:  A$160.33
You save:  A$48.10
S$95.14
List Price:  S$135.93
You save:  S$40.78
HK$543.62
List Price:  HK$776.63
You save:  HK$233.01
CHF 63.04
List Price:  CHF 90.06
You save:  CHF 27.02
NOK kr797.48
List Price:  NOK kr1,139.30
You save:  NOK kr341.82
DKK kr502.49
List Price:  DKK kr717.87
You save:  DKK kr215.38
NZ$124.10
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,361.55
List Price:  ৳11,945.58
You save:  ৳3,584.03
₹5,962.79
List Price:  ₹8,518.63
You save:  ₹2,555.84
RM314.04
List Price:  RM448.65
You save:  RM134.61
₦108,335.42
List Price:  ₦154,771.52
You save:  ₦46,436.10
₨19,495.71
List Price:  ₨27,852.21
You save:  ₨8,356.49
฿2,390.81
List Price:  ฿3,415.60
You save:  ฿1,024.78
₺2,467.44
List Price:  ₺3,525.07
You save:  ₺1,057.62
B$434.14
List Price:  B$620.23
You save:  B$186.09
R1,296.68
List Price:  R1,852.48
You save:  R555.80
Лв131.68
List Price:  Лв188.12
You save:  Лв56.44
₩102,198.93
List Price:  ₩146,004.73
You save:  ₩43,805.79
₪256.56
List Price:  ₪366.53
You save:  ₪109.97
₱4,090.81
List Price:  ₱5,844.26
You save:  ₱1,753.45
¥10,994.99
List Price:  ¥15,707.80
You save:  ¥4,712.81
MX$1,408.98
List Price:  MX$2,012.91
You save:  MX$603.93
QR255.09
List Price:  QR364.43
You save:  QR109.34
P967.82
List Price:  P1,382.66
You save:  P414.84
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,562.49
List Price:  E£5,089.49
You save:  E£1,527
ብር8,907.19
List Price:  ብር12,725.10
You save:  ብር3,817.91
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,323.69
List Price:  CLP$99,038.09
You save:  CLP$29,714.40
CN¥510.80
List Price:  CN¥729.75
You save:  CN¥218.94
RD$4,245.36
List Price:  RD$6,065.06
You save:  RD$1,819.70
DA9,465.07
List Price:  DA13,522.10
You save:  DA4,057.03
FJ$162.39
List Price:  FJ$232
You save:  FJ$69.60
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,638.77
List Price:  GY$20,913.42
You save:  GY$6,274.65
ISK kr9,772.70
List Price:  ISK kr13,961.60
You save:  ISK kr4,188.90
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,143.97
List Price:  ден5,920.21
You save:  ден1,776.24
MOP$559.97
List Price:  MOP$800
You save:  MOP$240.02
N$1,289.45
List Price:  N$1,842.15
You save:  N$552.70
C$2,574.80
List Price:  C$3,678.44
You save:  C$1,103.64
रु9,534.71
List Price:  रु13,621.60
You save:  रु4,086.89
S/260.99
List Price:  S/372.86
You save:  S/111.86
K283.85
List Price:  K405.52
You save:  K121.66
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,936.45
List Price:  ZK2,766.47
You save:  ZK830.02
L335.12
List Price:  L478.77
You save:  L143.64
Kč1,694.03
List Price:  Kč2,420.15
You save:  Kč726.12
Ft27,771.44
List Price:  Ft39,675.18
You save:  Ft11,903.74
SEK kr774.38
List Price:  SEK kr1,106.30
You save:  SEK kr331.92
ARS$71,792.58
List Price:  ARS$102,565.23
You save:  ARS$30,772.64
Bs484.55
List Price:  Bs692.24
You save:  Bs207.69
COP$307,165.41
List Price:  COP$438,826.54
You save:  COP$131,661.12
₡35,530.54
List Price:  ₡50,760.09
You save:  ₡15,229.55
L1,777.33
List Price:  L2,539.15
You save:  L761.82
₲546,250
List Price:  ₲780,390.60
You save:  ₲234,140.59
$U3,115.91
List Price:  $U4,451.49
You save:  $U1,335.58
zł287.82
List Price:  zł411.19
You save:  zł123.37
Already have an account? Log In

Transcript

In this video, we are going to understand about complex transformations. That is step by step. This video has a lot of information, please watch it carefully is very important. So, let us get started. So here I'll be working on this project Transformers complex. I already have flow main flow, where I have a transformers.

Now the requirement is I have a sample here in SRC main surface products dot JSON. This JSON is a complex Jason, which has an array of products. In our earlier video, we saw how to transform one single product. But now here, we'll see how to iterate over this array and how to actually iterate From the data, so I know how to sample file. So what I can do is right now I can use a sample for my main transform message here. For the payload, I can click on define data Connect.

I'll give the name products. Jason. And I know it's right. Jason. So I select Jason. I have an example.

So in select example and point the example in SRC main resources, okay. I should show the product details. Now it gets resolved. The metadata you can observe on the right corner after some time it will show you how the payload will look like see the payload is an array of products. Now, I want this sample data I like to edit sample data whatever I gave as an example it is shown us sample data Okay. Now, click on preview right now you can see the expression is empty curly braces means it is a map right when I can work a job.

Yes, see the preview is a map but an empty map. Now, I want to convert to Jason Firstly, so select Jason. Okay. Now the preview again change to American Jason What I do is I simply say products for payload. photos on the left side, I can see a same thing on the right side because the left side Jason world is just converted, just look like every product is having a brand name. So if I want to get all the brand names, payload dot, I say, brand name, see, failure dot brand name is a fourth type.

And I'll get an array of all the brand names for the products. Great. So that actually you should know what is an array indeed, in database. If I just write like this empty parenthesis for the empty brackets like this, it is an array. So if we rate again, fail brand name. payload dot brand name itself is an array an array inserted right now what I'll also do is I want to get the offer price so I'll just write payload doc offer is an array again inside offer price see I got this is another area of tourists right okay now you just understood how to actually get rid of elements by using dot operator.

Okay. Now what I want to do is just save payload and convert it to Java. See it shows the types of the values like product IDs and income Leaving a string like that is showing the money converter job. So, left side the JSON list of objects. So on the right side if it is convert to Java, we can see what let's type error list our list of maths findings in a list of maths Okay, now I want to iterate over the list before I hit read is right slash dw s output monitor. Actually, we're converting the left side Jason to dw type applications less GW is a MIME type.

It just looks like whatever is on the left side isn't. Is there any difference? You see? Yeah. Actually on the keys here on the left for the keys in double code, but here on the right side, there is no double quote. Basically what this transform message will do is put our expression new right here.

First will be converted to dw format, which looks like this exactly legit, Jason, but only differences keys are not quite to be wrapped inside neural code. Okay. Then actually suppose if I convert the output to JSON first what happens is that transform message, it first convert the written expression into dw format, and then finally convert it to Jason. Okay. Now that is understood. I want to actually show everything in dw right now.

I made it dw. Now I want to iterate over a map. So I want to iterate over this array on the left side. So it returns an array, there is an operator colors map. So map operator iterates over an array on the left side. And for each element in the array, it'll execute the expression after the map.

So right now, actually, I have six products. And for six products for each product, it has it rated and it is emitted. map. So I can see stuff empty maps as an output. Now, inside this I want to view p ID, product ID. achill to whenever it is iterating through each element, dollar represents the current element.

I'll say productivity so map Better will it read our correction on the left side and for each element emit this expression whenever you whenever it is looping if you want to access the current element of the look dollar visa current element, then similarly what I do is I want to get the name so, brand name I want to get brand name, dollar brand name okay I also want to get the offer price and say we'll be appraisal dollar dot see on the left side product is having offer and offers having offer price so if I want to get offer price that is not offered again, dollar dot offer dot offer Right. So now you can see where it is converted to dw. It's looking like this. Now I'll convert back to Jason The only difference is the keys are wrapped inside right now, have you understood how to create or each element by using map operator and how to extract the current element by using dollar Okay.

Now, what I want is I want this expression to be evaluated and stored in a variable. So, actually how to create a variable very simple. On the top for dash dash dash what we can do is we can create a variable where products is equals to the kill sins This is a list of products I will take this expression And then here I'll seem to say products. That's all. Now I can see the same output. Great.

Now, I want to create few more variables. Bad. Object one is equals to an empty map. Right? I just read key value pairs. So I'll read a key with a new one.

Colon value is one, comma w two colon. So basically, this is a simple object. I can say it's like a map key value pairs. I'll create this variable as object one. I want to create one more variable of type list add object to is equals to a list. Then I want to initialize it with the stuff to map.

So I would like to do stuff to it objects. Okay? These are common stuff to objects. And what I'll do is I'll read this one to the first one. And also in second one I'll write this three column. Okay?

Now I have two objects. I want to write a simple expression. So firstly, what I'll do is I'll click on this one source and its race. Yeah. No, no They have the preview right on the right side. It looks good.

And I use click on this, I can see only the expression and the preview. Okay. Now what I'll do is I'll simply say object one. Let's see, we'll look at this blade. Object one. Yep, exactly the left side map is printed for exactly the value map is printed.

As Jason, what I try to do is I'll try to keep this inside of parenthesis. Object one. It's giving you an error, because object one itself is having curly currently, right if you want to extract only key value pairs insert an object, what you can do is actually, you can wrap them, you can wrap an object inside parenthesis, if you wrapped up any object inside parenthesis what will happen is the object will be evaluated and only key value base will be extracted and given. So the output of this evaluated object one is just key value pairs, and they're wrapped inside. So remember, whenever we are wrapping an object, inside parenthesis, the object will be evaluated and key value pairs will be extracted. That's fine.

Now one more thing, I object to. So again, if you object to again this layer, actually object is an error right? Let us see what will happen if I play parenthesis to an array. You can just see when I applied parenthesis to an array, again that is evaluated the object is evaluated and the two objects are merging actually the two objects are module I got all the key value pairs. So remember when I applied parenthesis, power, aerialist. I'll get a flattened key value pairs all the key value pairs flattened.

So I got five key value pairs right? So remember the same now, what I'll do is I'll simply write products. So products is an eraser. He for this inside parenthesis, what was Get. So, this is evaluated and you can see that I got flattened the key value pairs but again, this is rap insider list is that okay? I will actually try to write like this.

See, here's an empty map insert is ever tried read products. What is happening? products is a list. If I'm ready to put insert a map, this will be key value pairs right. So what I want to do is I'll apply paranthesis this one, so what will happen if we apply a parenthesis to an added all the key value pairs will be flattened and I'll be getting a single object with key value pairs all the key value pairs like this and said okay now what I want to do is I'll apply map operator orders on our place So, I got all this given up so what I want is I want these three to be wrapped inside a product, it will be good is it so what I'll do is as of now, in this map itself, I will try to write right ducked colon and got it and sorry and then we should hear Yeah.

Now I got all this right I will try to convert this to XML is giving him a right whenever converting to XML there should be a root tag is it so firstly what I'll do is I just write I want products as a root tag products Okay, so mt tech And inside this if I write the variable for ducks chill I want this products variable to be evaluated to just avoid any confusions I write my products because it surface products and I get this. So here also late my products Oh my production is giving an error because my products is a list or an array right when this is evaluated to evaluate to an error. If we want all the key value pairs wrapped inside parenthesis Don't worry if you are getting confused about parenthesis please go back and see the video again step by step you will understand don't get confused So, If I apply parentheses on an array, I got each product as an XML like this over it very nice.

Now, what I want is I want only HP brands products whose brand is hp hp. So there's only one HP product. So we'll just use another another operator philosophy. Filter what again, dollar dot product dot brand name is equals to. I'll keep it here in Singapore hp. See I got only one product.

So better what I'll do is I'll create another variable that Free Code products is a request to and urine right. filtered products. I got the same out. Is it okay now I will switch it back to my products. Good all the products actually hear a hardcourt the brand name is that first of all let me go to cricket product itself got an HP printer but I don't know hardcore If you see on the input metadata, just click on this, if you see on the map input metadata, since on my left side of transform message, since I have HTTP listener, I'm able to see the metadata about what all attributes you can see all the HTTP attributes in the input. Let's assume that I don't want to hard code.

This PHP. I want to, I'm expecting it from a query parameter. So how to access query parameters. You're a candidate use attributes dot query params for query fathoms dot, let's say I'm expecting weaning. Yep. So I initialized with a variable.

I'm extracting the value dynamically. I'm not hard coding HP directly here. Is it? So better I keep this in a parenthesis let us see. Okay. But I'm not able to see the preview here you can see right?

Because whenever I'm actually filtering this value is dynamic. How can I get a preview? If you still want to get a preview, what you can do is expect a query parameter has been inserted. Here on the left side attributes query params Because I can click or click on it. And I will view sample data. So actually, I would right click on attributes.

Any sample data attributes SQL databases. Okay, I clicked on an example data. Yeah. Here are the attributes. So key value bits, key value pairs. Actually, you can see the extension is dw you're writing a sample file as dw l file.

He valid business in dw. These are one. And inside attributes, you have key colors. What? query patterns, okay, giving sample data. So really, Adams is a key and the value is again a map that can be a number of query parameters.

So I'm expecting a query parameter Class B name and its value Anyways, let's say hp. So this is a hardcore dw. Now I gave a sample query parameter v name with value hp. So I got it. So basically in your expressions if you are using any dynamic expression like this, and if you want to do sample data, yes, it's easy, you can just go to the input, right click on whatever key you want go sample data, added sample data, the sample data right now should be a dw. I wrote a simple dw l variable very params as a key like this.

Now I got the preview. Great. So to now you understood how to actually use a filter for filtering and also how to filter dynamically. Now, how to start very simple. I'll go and modify this to display all the products you can see I got all the products, but the offer price the first perk is thousand second is 3000 again 2000. So, I want all the products to be displayed in ascending order based on the price.

So what I can do is same on this product I'll apply ordered by dollar dot product dot o p i want to order by this one. So see you can see thousand thousand I got all of them in ascending mode, series o p is in number. So right now I got all the products in ascending order how to get all of this Products again in descending order. It's very simple. Just use order by and then minus, minus. So now they are actually arranged in descending order.

Okay, so now you understood how to honor one. So again as a best practice are always said to create whenever you're writing some expression, create a variable where order products is it close to and I take it here right now here I can simply right ordered products even in the applying filter on my products I can apply some order products same thing now. The filtered products will be in our great so now you understood how to filter and how to order wine. I will see you in next video. There is no lab right now. After next video, you can do a single lab combined.

I'll see you next video.

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.