Writing Complex transformations-part3

Mule 4 Indepth DataWeave2 Transformations
15 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.28
List Price:  €96.12
You save:  €28.84
£55.93
List Price:  £79.91
You save:  £23.97
CA$98.01
List Price:  CA$140.02
You save:  CA$42.01
A$107.87
List Price:  A$154.11
You save:  A$46.23
S$94.42
List Price:  S$134.90
You save:  S$40.47
HK$544.78
List Price:  HK$778.29
You save:  HK$233.51
CHF 62.22
List Price:  CHF 88.89
You save:  CHF 26.67
NOK kr779.46
List Price:  NOK kr1,113.56
You save:  NOK kr334.10
DKK kr501.79
List Price:  DKK kr716.88
You save:  DKK kr215.08
NZ$119.98
List Price:  NZ$171.41
You save:  NZ$51.43
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,402.58
List Price:  ৳12,004.20
You save:  ৳3,601.62
₹5,911.93
List Price:  ₹8,445.97
You save:  ₹2,534.04
RM312.68
List Price:  RM446.70
You save:  RM134.02
₦118,358.68
List Price:  ₦169,091.08
You save:  ₦50,732.40
₨19,451.98
List Price:  ₨27,789.74
You save:  ₨8,337.75
฿2,421.30
List Price:  ฿3,459.15
You save:  ฿1,037.85
₺2,419.75
List Price:  ₺3,456.94
You save:  ₺1,037.18
B$406.81
List Price:  B$581.19
You save:  B$174.37
R1,266.21
List Price:  R1,808.95
You save:  R542.74
Лв131.43
List Price:  Лв187.77
You save:  Лв56.33
₩98,455.31
List Price:  ₩140,656.47
You save:  ₩42,201.16
₪260.61
List Price:  ₪372.31
You save:  ₪111.70
₱4,125.84
List Price:  ₱5,894.31
You save:  ₱1,768.47
¥10,813.84
List Price:  ¥15,449
You save:  ¥4,635.16
MX$1,432.62
List Price:  MX$2,046.69
You save:  MX$614.07
QR256.35
List Price:  QR366.23
You save:  QR109.88
P959.91
List Price:  P1,371.36
You save:  P411.45
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,476.67
List Price:  E£4,966.88
You save:  E£1,490.21
ብር8,762.26
List Price:  ብር12,518.05
You save:  ብር3,755.79
Kz63,877.12
List Price:  Kz91,256.94
You save:  Kz27,379.82
CLP$68,152.06
List Price:  CLP$97,364.26
You save:  CLP$29,212.20
CN¥507.37
List Price:  CN¥724.85
You save:  CN¥217.47
RD$4,236.71
List Price:  RD$6,052.70
You save:  RD$1,815.99
DA9,355.50
List Price:  DA13,365.57
You save:  DA4,010.07
FJ$159.32
List Price:  FJ$227.62
You save:  FJ$68.29
Q542.77
List Price:  Q775.43
You save:  Q232.65
GY$14,710.67
List Price:  GY$21,016.15
You save:  GY$6,305.47
ISK kr9,775.50
List Price:  ISK kr13,965.60
You save:  ISK kr4,190.10
DH703.21
List Price:  DH1,004.63
You save:  DH301.41
L1,276.61
List Price:  L1,823.81
You save:  L547.19
ден4,112.73
List Price:  ден5,875.58
You save:  ден1,762.85
MOP$563.70
List Price:  MOP$805.33
You save:  MOP$241.62
N$1,272.29
List Price:  N$1,817.64
You save:  N$545.34
C$2,573.53
List Price:  C$3,676.63
You save:  C$1,103.10
रु9,461.06
List Price:  रु13,516.38
You save:  रु4,055.32
S/267.08
List Price:  S/381.56
You save:  S/114.48
K283.05
List Price:  K404.37
You save:  K121.32
SAR262.80
List Price:  SAR375.44
You save:  SAR112.64
ZK1,938.90
List Price:  ZK2,769.98
You save:  ZK831.07
L334.76
List Price:  L478.25
You save:  L143.49
Kč1,707.89
List Price:  Kč2,439.96
You save:  Kč732.06
Ft27,663.65
List Price:  Ft39,521.19
You save:  Ft11,857.54
SEK kr779.03
List Price:  SEK kr1,112.95
You save:  SEK kr333.92
ARS$70,272.32
List Price:  ARS$100,393.34
You save:  ARS$30,121.01
Bs485.83
List Price:  Bs694.07
You save:  Bs208.24
COP$310,661.31
List Price:  COP$443,820.90
You save:  COP$133,159.58
₡35,743.76
List Price:  ₡51,064.70
You save:  ₡15,320.94
L1,776.85
List Price:  L2,538.46
You save:  L761.61
₲551,865.53
List Price:  ₲788,413.13
You save:  ₲236,547.59
$U2,996.30
List Price:  $U4,280.61
You save:  $U1,284.31
zł292.33
List Price:  zł417.63
You save:  zł125.30
Already have an account? Log In

Transcript

Welcome back. In this video, we are going to understand about how to transform the XML input. Till now we have transformed JSON input. But now we'll see how to transform the XML input. Okay, so now I have this sample XML, I'm expecting the data to be in the form of XML like this, where I have route Tigers products, and a lot of product tags. Let's see how to transform this JSON to solve this XML to JSON.

Very simple. Again, I'll be working on this transforming XML. Here I have an empty flow. And I will define the payload metadata click on define and say my Redux XML Here I know the type is XML. I have an example. I'll point it to SRC main resources.

Products dot XML. Okay detected. So on the left side is showing payload under that we have products and we have product star repeated things. So let's see sample data, right click in sample data. Yeah, this is a sample data. Now, simply, I want to convert it to Java as a payload and I want to see the preview.

You can see the preview Actually, it is containing products. Preview is like a map. its kinetic products. How many product is showing actually here? Only one product. How come?

Okay, let's continue. And this product actually it's showing it as a map. Again, right? This is the raw data, whatever you're seeing down is the raw data and this is the actual converted object. So you can see that output is nothing but a map map. Because when you're converting an XML, the root action will always be converted to map only because root tag will be taken as a key and body will be the value every tag will be a key toggling we will a key and the body of the tag will be the value like that here it is taken products is our key and water is the body is a very in that product is a key and again it has a board like that right?

But actually tagname is same right? Can you guess why only one product is played in output in the map? Actually this is a map or the key tagname tag name is a key for each product the key same for each product is same if we put things inside the lab what will happen, the value will be overwritten is that so you can see actually the value of the productivity six. So last Friday is what we are seeing because the key is Same for the map whenever we are trying to put in a map or try to convert it to dw let us see now. Yep, now you can see the preview. Here, when I'm converting to dw, that's okay, I got all the products, product with ID one product with ID, two, three, etc.

When I'm converting just with the left side to right side, it's it's like every product is visible here, right? But when I convert with Java, since in Java, it's converted to a map. Since it's converted map, this is output, the actual output only one product is there because what will become the key inside this map tagname if there are six products for all the six products, the tablets product on. So all the values got overridden and we are seeing only the last product. Okay? What I'll do is I said product payload dot, the keys products.

Product if I say I'm getting an S zero hash. Actually, whenever you have a tag belonging to namespace we are to actually give information about the namespace here. I can read ns ns zero. What is a namespace is I'll copy this and paste it here. So now I'm telling that I'm referencing to product tag under this namespace. So On the top, you're specify LS and give an alias we'll say my ns, then here at use my and let's hash the referencing to prototype belongs to this namespace.

Okay? So once it's selected products see the preview? Well I got one product and it's a dot product. See I got only the key value pairs present inside first product actually want to stuff for products. So whenever you are whenever you want the repeated element, get you start, start. See now I got a list of all products.

So one difference you need to understand is whenever you are having repeated tags in an accent And if you want a list of all the repeat tags use star. This is one difference between XML and JSON processing. Now, this is a is a list and you got a list. You can store this in a variable, I'd say that products in question. And then here I'll simply write products. Now, product is a list.

Whatever operations we're doing in our earlier video, same thing are applicable. The only difference is whenever you are working on XML input, and if you want a list out of repeated elements or just rest Allah, same. That's all what I want to talk about XML transformations. Now. Let's see. The types data types in dw.

If you see I have opened this dw types where I have some configuration already. Now here, you are seeing the left side is an xml. xml is converted to Java type by iterating over it using a map. If I see the preview, see here, one is considered to be a string, all our strings only. But I know one, PhD is a number, how to do typecasting. It's very simple.

You say explicitly as number one, you know it's a digit number. Right? See here now as number and we got integer Similarly offered price also is a number heroes I'll use as number nine number is a predefined type. Okay? And what about offer valid until, for violently looks like to be a date, but here I'm getting it as a string. So how to convert it to data object as the now you can see after voluntas is converted to data object that but actually whenever you're converting string to date you tell what to expect input format I didn't tell but still it worked because by default the expected input format is what bye bye bye bye mmm DD but let's see you're getting data in different format.

How does Specify the input format. What you can do is can just use like this format alright yyyy mm DD. So this is how you have to specify the input format by default even if you don't specify the format also the sense that's right. But if you are date format is something else you have to specify the format like this. Let's assume that in my dw has a lot of places where I want to write where I have screen to get conversions every place I have to specify the format and date like this regard. If I can define my own type is it so We can define our own type, how to define our own type, it's very simply a trade type my day is a custom date and I just copy paste this format here.

So, I define my own date my own type and widescreen format also, whenever I want to use same conversion same string to date conversions everywhere I can use my date. Now, if you understood how to convert, how to actually define your own types, okay, but now you can see that offer is of rate map and this whole thing also is of type. Actually, I have a Java class in the same project called as offer offer is having two variables offer price offer value until offer prices double and approval endless data I don't want to hear an object of type nap for offer I want it to be class so what I can do is I can different type here type of the object here what I can do is for offer I don't want it to be a map I want to be an object so as object I can specific class never code comm dot line dot offer yeah I saved it the non landed offer Yeah.

So is my whole class name. As you can see that this offer tag is converted to offer. Similarly if I want the whole list of products to be converted, list of IDs of map I want product so what I can do is as object and here I can specify class way to react but when I'm converting to product it's not converting it's giving an error. Wait, because here if I see the keys are up ID name right. The keys should match the properties here I have product ID as a key. so here also whenever I'm creating who the key should be same as expected in the class.

And what is product name? Name is same. brand name is same. And restaurant looks the same. Let us see the preview right now. Is there anything else to be changed?

Yep, nauseous. Now you can see here the preview is an object of products. So now we understood how to specify our own types, number, date, product, etc. And this is very specific when you're converting to Java when you're converting to Jason doesn't make any sense to convert this product can offer. All right, so whenever you're converting to Java itself, should be moving to a specific about what is the type of the variable and the class types. So, these are we can define the types you can define your custom type and you can type cast also.

Right. So great. In this video, you understood XML transformations and also typecasting how to define your own types. Now, you have a lab, you have to open this document level Transformers complex. There are step one in step two and step three is to complete all the steps. This is a very good exercise.

Please don't skip this exercise. Do as per the document. See when 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.