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
€66.52
List Price:  €95.03
You save:  €28.51
£55.40
List Price:  £79.15
You save:  £23.74
CA$97.77
List Price:  CA$139.67
You save:  CA$41.90
A$107.48
List Price:  A$153.55
You save:  A$46.07
S$94.07
List Price:  S$134.39
You save:  S$40.32
HK$544.73
List Price:  HK$778.22
You save:  HK$233.49
CHF 61.85
List Price:  CHF 88.36
You save:  CHF 26.51
NOK kr773.61
List Price:  NOK kr1,105.21
You save:  NOK kr331.59
DKK kr496.19
List Price:  DKK kr708.87
You save:  DKK kr212.68
NZ$119.31
List Price:  NZ$170.45
You save:  NZ$51.14
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,370.95
List Price:  ৳11,959.01
You save:  ৳3,588.06
₹5,916.42
List Price:  ₹8,452.40
You save:  ₹2,535.97
RM312.50
List Price:  RM446.45
You save:  RM133.95
₦117,624.49
List Price:  ₦168,042.19
You save:  ₦50,417.70
₨19,473.08
List Price:  ₨27,819.88
You save:  ₨8,346.79
฿2,426.48
List Price:  ฿3,466.55
You save:  ฿1,040.07
₺2,418.28
List Price:  ₺3,454.83
You save:  ₺1,036.55
B$406.67
List Price:  B$580.99
You save:  B$174.31
R1,268.60
List Price:  R1,812.36
You save:  R543.76
Лв130.09
List Price:  Лв185.85
You save:  Лв55.76
₩98,070.61
List Price:  ₩140,106.88
You save:  ₩42,036.26
₪261.37
List Price:  ₪373.41
You save:  ₪112.03
₱4,133.88
List Price:  ₱5,905.80
You save:  ₱1,771.92
¥10,806.63
List Price:  ¥15,438.71
You save:  ¥4,632.07
MX$1,423.91
List Price:  MX$2,034.25
You save:  MX$610.33
QR255.44
List Price:  QR364.93
You save:  QR109.49
P956.94
List Price:  P1,367.12
You save:  P410.17
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,475.04
List Price:  E£4,964.56
You save:  E£1,489.51
ብር8,733.50
List Price:  ብር12,476.97
You save:  ብር3,743.46
Kz63,853.79
List Price:  Kz91,223.61
You save:  Kz27,369.81
CLP$68,157.66
List Price:  CLP$97,372.26
You save:  CLP$29,214.60
CN¥506.84
List Price:  CN¥724.09
You save:  CN¥217.25
RD$4,224.90
List Price:  RD$6,035.83
You save:  RD$1,810.93
DA9,349.94
List Price:  DA13,357.64
You save:  DA4,007.69
FJ$158.89
List Price:  FJ$226.99
You save:  FJ$68.10
Q540.76
List Price:  Q772.55
You save:  Q231.78
GY$14,655.30
List Price:  GY$20,937.04
You save:  GY$6,281.74
ISK kr9,679.61
List Price:  ISK kr13,828.61
You save:  ISK kr4,149
DH699.78
List Price:  DH999.72
You save:  DH299.94
L1,274.52
List Price:  L1,820.82
You save:  L546.30
ден4,093.48
List Price:  ден5,848.08
You save:  ден1,754.59
MOP$561.60
List Price:  MOP$802.33
You save:  MOP$240.72
N$1,270.52
List Price:  N$1,815.10
You save:  N$544.58
C$2,577.69
List Price:  C$3,682.57
You save:  C$1,104.88
रु9,455.06
List Price:  रु13,507.81
You save:  रु4,052.75
S/265.82
List Price:  S/379.77
You save:  S/113.94
K281.99
List Price:  K402.86
You save:  K120.87
SAR262.75
List Price:  SAR375.38
You save:  SAR112.62
ZK1,938.58
List Price:  ZK2,769.51
You save:  ZK830.93
L331.04
List Price:  L472.94
You save:  L141.89
Kč1,685.42
List Price:  Kč2,407.85
You save:  Kč722.43
Ft27,354.11
List Price:  Ft39,078.98
You save:  Ft11,724.86
SEK kr771.99
List Price:  SEK kr1,102.90
You save:  SEK kr330.90
ARS$70,181.20
List Price:  ARS$100,263.16
You save:  ARS$30,081.95
Bs485.08
List Price:  Bs693
You save:  Bs207.92
COP$308,906.07
List Price:  COP$441,313.30
You save:  COP$132,407.23
₡35,572.52
List Price:  ₡50,820.06
You save:  ₡15,247.54
L1,770.13
List Price:  L2,528.86
You save:  L758.73
₲548,589.81
List Price:  ₲783,733.33
You save:  ₲235,143.51
$U2,994.50
List Price:  $U4,278.05
You save:  $U1,283.54
zł289.22
List Price:  zł413.19
You save:  zł123.97
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.