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
€65.59
List Price:  €93.70
You save:  €28.11
£56.03
List Price:  £80.05
You save:  £24.01
CA$96.39
List Price:  CA$137.71
You save:  CA$41.31
A$107.98
List Price:  A$154.26
You save:  A$46.28
S$95.50
List Price:  S$136.44
You save:  S$40.93
HK$547.57
List Price:  HK$782.28
You save:  HK$234.70
CHF 64.43
List Price:  CHF 92.05
You save:  CHF 27.61
NOK kr776.82
List Price:  NOK kr1,109.79
You save:  NOK kr332.97
DKK kr489.21
List Price:  DKK kr698.91
You save:  DKK kr209.69
NZ$118.81
List Price:  NZ$169.74
You save:  NZ$50.92
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,729.18
List Price:  ৳11,042.16
You save:  ৳3,312.97
₹5,842.93
List Price:  ₹8,347.41
You save:  ₹2,504.47
RM333.67
List Price:  RM476.70
You save:  RM143.02
₦92,745.84
List Price:  ₦132,499.74
You save:  ₦39,753.90
₨19,599.98
List Price:  ₨28,001.17
You save:  ₨8,401.19
฿2,600.12
List Price:  ฿3,714.62
You save:  ฿1,114.50
₺2,271.31
List Price:  ₺3,244.87
You save:  ₺973.56
B$363.64
List Price:  B$519.51
You save:  B$155.87
R1,308.28
List Price:  R1,869.05
You save:  R560.77
Лв128.30
List Price:  Лв183.29
You save:  Лв54.99
₩96,996.60
List Price:  ₩138,572.52
You save:  ₩41,575.91
₪261.41
List Price:  ₪373.46
You save:  ₪112.04
₱4,045.59
List Price:  ₱5,779.67
You save:  ₱1,734.07
¥11,052.75
List Price:  ¥15,790.32
You save:  ¥4,737.57
MX$1,197.81
List Price:  MX$1,711.23
You save:  MX$513.42
QR257.31
List Price:  QR367.61
You save:  QR110.29
P1,000.11
List Price:  P1,428.79
You save:  P428.68
KSh9,335.98
List Price:  KSh13,337.69
You save:  KSh4,001.70
E£3,349.95
List Price:  E£4,785.86
You save:  E£1,435.90
ብር4,013.63
List Price:  ብር5,734.01
You save:  ብር1,720.37
Kz58,409.45
List Price:  Kz83,445.65
You save:  Kz25,036.20
CLP$67,298.07
List Price:  CLP$96,144.23
You save:  CLP$28,846.15
CN¥506.79
List Price:  CN¥724.02
You save:  CN¥217.23
RD$4,121.81
List Price:  RD$5,888.55
You save:  RD$1,766.74
DA9,423.87
List Price:  DA13,463.25
You save:  DA4,039.38
FJ$159.96
List Price:  FJ$228.52
You save:  FJ$68.56
Q547.38
List Price:  Q782
You save:  Q234.62
GY$14,742.16
List Price:  GY$21,061.13
You save:  GY$6,318.97
ISK kr9,832.89
List Price:  ISK kr14,047.59
You save:  ISK kr4,214.70
DH712.24
List Price:  DH1,017.53
You save:  DH305.29
L1,243.01
List Price:  L1,775.81
You save:  L532.79
ден4,046.68
List Price:  ден5,781.23
You save:  ден1,734.54
MOP$564.08
List Price:  MOP$805.87
You save:  MOP$241.78
N$1,315.96
List Price:  N$1,880.02
You save:  N$564.06
C$2,592.36
List Price:  C$3,703.54
You save:  C$1,111.17
रु9,349.94
List Price:  रु13,357.63
You save:  रु4,007.69
S/263.70
List Price:  S/376.73
You save:  S/113.03
K271.96
List Price:  K388.53
You save:  K116.57
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,886.12
List Price:  ZK2,694.57
You save:  ZK808.45
L326.39
List Price:  L466.29
You save:  L139.90
Kč1,650.20
List Price:  Kč2,357.53
You save:  Kč707.33
Ft25,629.21
List Price:  Ft36,614.73
You save:  Ft10,985.52
SEK kr770.71
List Price:  SEK kr1,101.06
You save:  SEK kr330.35
ARS$61,698.11
List Price:  ARS$88,143.94
You save:  ARS$26,445.82
Bs486.86
List Price:  Bs695.54
You save:  Bs208.68
COP$271,471.23
List Price:  COP$387,832.67
You save:  COP$116,361.43
₡35,819.54
List Price:  ₡51,172.97
You save:  ₡15,353.42
L1,740.31
List Price:  L2,486.27
You save:  L745.95
₲526,285.13
List Price:  ₲751,868.13
You save:  ₲225,582.99
$U2,699.91
List Price:  $U3,857.19
You save:  $U1,157.27
zł284.80
List Price:  zł406.88
You save:  zł122.07
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.