Creating and Using Key-Value Lookups in MuleSoft (Mule 4)
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 such fields will hardly change in a real-time scenario. We can create a csv file(country_codes.csv) as shown below.
CSV File- country_codes.csv
India,IN
Indonesia,ID
Philippines,PH
United States,US
Russia,RU
Netherlands,NL
Czech Republic,CZ
From the source system, we are getting an address payload as shown below.
Source Payload
"address": {
"streetAdd": "street1",
"streetAdd2": "address2",
"city": "City1",
"state": "State1",
"country": "Indonesia",
"zip": "123456"
}
}
Our target system expects only country code to be sent in the payload. Below are the steps to accomplish this
STEPS:
Output Payload
Similarly, we can extend the function and the csv file to fetch values with multiple inputs and conditions.
References:
Please share your valuable feedback 😊😊😊
Thanks for sharing this,Looking for this topic
ReplyDelete