Skip to main content Skip to complementary content
Close announcements banner

Returning a loop index

Use a LoopIndex function to return the index for a loop and its parent.

Before you begin

You have created a map with an input and an output structure. You can use the samples provided below.

About this task

In this example, you have a JSON file containing information about team managers and employees. You want to create an XML file with employee information, including a unique employee index and a team index. You can use the following JSON sample as input:
{
   "team":[
      {
         "manager":"Steve Rogers",
         "employees":[
            "Tony Stark",
            "Natasha Romanoff",
            "Bruce Banner",
            "Clint Barton"
         ]
      },
      {
         "manager":"Reed Richards",
         "employees":[
            "Susan Storm",
            "Johnny Storm",
            "Ben Grimm"
         ]
      }
   ]
}
You can use the following XML sample to create your output structure.
<root>
	<employee>
		<employeeName/>
		<employeeIndex/>
		<teamIndex/>
	</employee>
	<employee>
		<employeeName/>
		<employeeIndex/>
		<teamIndex/>
	</employee>
</root>

Procedure

  1. Click the output employee element to configure its loop expression:
    1. Drag and drop a SimpleLoop function in the Loop tab.
    2. Drag and drop the input team element on the Input Map Element argument.
    3. Drag and drop a NestedLoop function on the Contexts argument and drop another SimpleLoop function on it.
    4. Drag and drop the input employees element on the Input Map Element argument.
  2. Drag and drop the input employees element on the output employeeName.
  3. Drag and drop a LoopIndex function on the employeeIndex element.
  4. Drag and drop the output employee element on the Looping Output Element argument.
  5. Double-click the LoopIndex and select Output Loop in the Use Index Of drop-down list and click OK.
    This indicates that the element should return the index corresponding to the iteration of the employee element in the output structure. If you select Input Loop, the index corresponds to the iteration of the input employees element within a team element.
  6. Drag and drop a LoopIndex function on the employeeIndex element.
  7. Drag and drop the output employee element on the Looping Output Element argument.
  8. Double-click the LoopIndex and select in$/employees_input/team in the Looping Index of Element and click OK.
    Since the employee element has a loop expression containing a nested loop, you can choose to return the index for any of the corresponding input loops. In this case, you want to return the index corresponding to the iteration of the input team element.
    Information noteNote: This feature is available only if you have installed the R2020-04 Studio monthly update or a later one delivered by Talend. For more information, check with your administrator.

Results

Your map in now configured. You can use the Test Run feature to see the result. In this example, the following output is returned:
<root>
  <employee>
    <employeeName>Tony Stark</employeeName>
    <employeeIndex>1</employeeIndex>
    <teamIndex>1</teamIndex>
  </employee>
  <employee>
    <employeeName>Natasha Romanoff</employeeName>
    <employeeIndex>2</employeeIndex>
    <teamIndex>1</teamIndex>
  </employee>
  <employee>
    <employeeName>Bruce Banner</employeeName>
    <employeeIndex>3</employeeIndex>
    <teamIndex>1</teamIndex>
  </employee>
  <employee>
    <employeeName>Clint Barton</employeeName>
    <employeeIndex>4</employeeIndex>
    <teamIndex>1</teamIndex>
  </employee>
  <employee>
    <employeeName>Susan Storm</employeeName>
    <employeeIndex>5</employeeIndex>
    <teamIndex>2</teamIndex>
  </employee>
  <employee>
    <employeeName>Johnny Storm</employeeName>
    <employeeIndex>6</employeeIndex>
    <teamIndex>2</teamIndex>
  </employee>
  <employee>
    <employeeName>Ben Grimm</employeeName>
    <employeeIndex>7</employeeIndex>
    <teamIndex>2</teamIndex>
  </employee>
</root>

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!