Learn by product

Select the product you would like help with:

Martini Logo
Select the product you would like help with:
Community Q&A

Ask the Community

I need to iterate an array, concatenate multiple properties and then map it into a property. e.g

 

<order>
   <alias>
       <first_name>aa</first_name>
       <last_name>aaa</first_name>
   </alias>
   <alias>
       <first_name>bb</first_name>
       <last_name>bbb</first_name>
   </alias>
   <alias>
       <first_name>cc</first_name>
       <last_name>ccc</first_name>
   </alias>
</order>

 The aliases are going to be mapped to a property called alternateNames like so:

 

alternateNames: aa aaa, bb bbb, cc ccc

 Any ideas?


You can use the Iterate Step to iterate over an array and map it to any object.


Using your example, we can create a service:


  1. Create a Gloop Service
  2. In the Input/Output view, add an input property, let's name it xmlExample as the example is in xml. Declare it as a String.
  3. Add an output property, let's name it alternateNames, declare it as a String and an Array.
  4. Convert the xml into a Gloop object by using the one-liner XmlMethods.xmlStringToGloopObject 
  5. Map the output of the one-liner to a model that looks like this:
  6. Using an Iterate Step, set the model.alias array as the input array and the alterateNames array as the output array.
  7. Create a map step as a child of the iterate step.
  8. In the map step, select the alternateNames property and open the Set Expression Editor and place this code into the dialog 
"${model.alias.first_name} ${model.alias.last_name}"


Now run the service and paste the example xml into the xmlExample property then check the output of the service. Looking at the JSON output, it should look like this:

{
    "alternateNames": [
        "aa aaa",
        "bb bbb",
        "cc ccc"
    ]
}


If you want to concatenate the contents of the array, you could use the Iterable method called .join(). So to complete your example, we will concatenate the contents of alternateNames by using the following code


alternateNames.join(', ')


then the value returned is:


aa aaa, bb bbb, cc ccc