Dataweave Interview Question: Assign ID to Every Name in an Array of Objects
This article will help you practice your DataWeave skills in MuleSoft. Here, we will assign an id to every name in an array of objects.
Join the DZone community and get the full member experience.Join For Free
This article will help you practice your DataWeave skills in MuleSoft. Here, we will assign an
id to every name in an array of objects. Let's get started.
Let's talk about the solution now. We are going to achieve this in multiple steps as follows:
We will find distinct names from the input and store them into a variable.
Step 1 Output:
In order to get output like this, we will use the below code:
We will first apply a map on distinctNames.
After that, we will filter payload where the name in payload matches the name in distinctNames.
After that, we will apply a map and create an object with id, name values with incrementing id.
At last, we will flatten to the whole output.
Step 2 code:
Hope this helps improve your DataWeave skills. I am not an expert on DataWeave. I am just sharing what I can solve. Suggestions and Comments are welcome. Thanks.
Opinions expressed by DZone contributors are their own.