Posts

Showing posts with the label DATAWEAVE

Experimenting Recursion in DataWeave - Fibonacci Series

Image
  In various programming languages, recursion is a process in which a method or function calls itself to simplify a complicated problem. Understanding recursive solution sometimes become difficult. Let's try to solve a mathematical problem through recursion with a twist by using DataWeave . The Fibonacci series is a generic use case that recursion can solve. This is a very generic question in programming interviews. Photo by Natalya Letunova on Unsplash What is Fibonacci Series? A Fibonacci series is a set of integers starting with 0 followed by 1, and a series continuing with the sum of the preceding two integers in the sequence. For example, F n =0,1,1,2,3,5,8,13,21,34... Mathematically, F 0 =0 F 1 =1 F n =F (n-1) + F (n-2) Now transforming the above logic in the form of a recursive function to find the nth digit in the Fibonacci series. DataWeave Recursive Function: %dw 2.0 output application/json fun fib_r(n)= if...

[Mule 4]Catch numeric exceptions inside DataWeave

Image
 Numbers are a simple data type, but become complex in real-time integration transformation scenarios. Number data type, we consider number system in base 10 and perform all kinds of arithmetic operations e.g. +, -, *, / etc. We do this starting in childhood: multiply two numbers, divide one number by another, or add two numbers. Now, where is the tricky part 🤔?  Let’s start with, various tricky aspects and possible remedies to these. This article is published on the MuleSoft Community  LinkedIn page. To continue reading please visit the below link. https://www.linkedin.com/pulse/mule-4catch-numeric-exceptions-inside-dataweave-mulesoftcommunity Please share your valuable feedback 😊😊😊

Number System Conversion in MuleSoft(Mule 4) using Dataweave

Image
 The number 11, we call it eleven in the decimal system. Still, it can be a representation of three in a binary system or it can be seventeen in a hexadecimal system. The actual value of a number depends on the base it is calculated. It is interesting changing the base changes the value of the representation. Let us assume in some integration flow we are getting values in binary number system but target system accepts values in a decimal system or hexadecimal system or vice versa. Examples depicting values in various number systems: # DECIMAL BINARY HEXADECIMAL OCTAL BASE 10 BASE 2 BASE 16 BASE 8 I 5 101 5 5 II 14 1110 E 16 III 54 110110 36 66 IV 123 1111011 7B 173 V 240 1111...

Creating and Using Key-Value Lookups in MuleSoft (Mule 4)

Image
Lookups or key-value pairs are really helpful while developing integrations. Suppose, we get a value from the source system but the target system requires a corresponding value for that key(incoming data). One way is to store such cross-reference in a database table and fetch values. What if, these values are not changed frequently, and accessing a database will probably pose an overhead. A better approach would be to have a static lookup file included within the Mule application. This lookup file can be a .csv, JSON, XML, Excel, etc. file. For simplicity, in this blog, we will be using CSV files as an example for creating and using lookups in Mule 4. A CSV file is easy to maintain and use. Let's start with the below use case. Let's take a use case of Country codes. From the source system, the country field contains Country Name e.g. India but our target system requires 2 character Country Code e.g. IN as input in one of the fields. Now ...

Playing with XML Namespaces in Mule 4 Dataweave

Image
 Today, the world is full of REST APIs which use JSON as a major message type. But we have a lot of applications and APIs in the middleware world which rely on XML message structures. XML messages are a bit more complex than JSON messages, as in XML messages namespaces play a vital role. A miss in the namespace can lead to errors that are sometimes unseen to normal eyes. In this blog, we will discuss various ways by which we can handle the transformation of XML messages with or without namespaces in Mule 4 using Dataweave. Transforming XML messages without namespaces Transforming XML messages with namespaces  1. Transforming XML messages without namespaces This conversion is pretty simple, all we need to consider is XPath and do the mapping accordingly. This is similar to the JSON data transformation Let's take an example of 2 XMLs, wherein both input and output XMLs don't have namespaces. The output we need to specify is application/xml . IN...

Tips and tricks to handle dateTime formats in Mule 4

Image
We generally come across various scenarios while developing integrations, wherein we have to deal with multiple formats of date, time, or DateTime. Sometimes source systems can send date data in only one format which may or may not be accepted by target systems, which we have to handle using dataweave expressions. In this blog, we will be going through a couple of generic scenarios which we come across frequently. For example, if we use now() in dataweave, we get an output as below. DataWeave expression: %dw 2.0 output application/json --- now() Output: "2021-12-12T14:27:48.064709Z" The above output is in the format of DateTime, and from this, it is easy to convert or extract and date or time values. Scenario #1 What if we get date time in some different format, for e.g. " 2021/12/12 14.27.48 " and we just need to extract date ...

Popular posts from this blog

DateTime formatting using xp20:format-dateTime ()

Create Delimited String from XML Nodes and Vice Versa in SOA 12c

Import and Export MDS artifacts in SOA 12c