MuleSoft — Make a dynamic index or key of an ArrayList or a Map
Often times in our projects we need to create an arraylist or map in which both key and values are dynamic. In this tutorial I will walk you through step by step using an example how we can achieve this using Mule’s functional programming Dataweave 2.0.
Step 1: Use an api which will give you a structure with multiple fields.
I will use the https://www.universal-tutorial.com/api/countries/ API and the response is something like this-
Step 2: Configure the http-request connector as per documentation which is provided in “https://www.universal-tutorial.com/rest-apis/free-rest-api-for-country-state-city”.
Response after successfully hitting the api
Each node of this response contain 3 fields which are country_name, country_short_name, country_phone_code. But we want a structure like
The country_name will be the key or index and country_phone_code will be the value.
Step 3: Add a transform-message connector like this,
After adding the transform message the response will be as follows:
That’s it, we just created a payload containing dynamic data.