# FOR_EACH function

## `FOR_EACH` function

| Function Name | Description | Usage | Input parameter | Return value |
|---|---|---|---|---|
| `FOR_EACH` | Applies one or more transformation functions for each element in an array. | `FOR_EACH(~objn -> Variable or Value)` Where **\~obj<var translate="no">n</var>** indicates the current array iteration element for which you want to apply the transformation functions. By default, the value for <var translate="no">n</var> starts from 1. For JSON arrays with schema, you can use a dot (.) notation following to the current array element to directly access the nested property of the that array element. See [example](https://docs.cloud.google.com/application-integration/docs/reference/data-mapping-reference/for-each-function-reference.md.txt#ex). | Transformation functions. | An array of elements. |

## Supported data type


The `FOR_EACH` function supports the following data types:

- Boolean array
- Double array
- Integer array
- JSON
- String array

## Example 1: Concat a value to all the elements of a string array.


**Sample data** : `$var1$ = {"Alex","Bola","Charlie","Dana","Hao"}`


**Usage** : `$var1$.FOR_EACH(~obj1-> ~obj1.CONCAT("@gmail.com"))`


Concat the value *@gmail.com* to *var1*.


**Output** :
`
{
"Alex@gmail.com",
"Bola@gmail.com",
"Charlie@gmail.com",
"Dana@gmail.com",
"Hao@gmail.com"
}
`

## Example 2: Given a JSON array without schema, add a property to all the JSON objects.


**Sample data**:

```
  $var1$ =
{
  "employeeDetails": [
    {
      "name": "Alex"
    },
    {
      "name": "Bola"
    },
    {
      "name": "Charlie"
    }
  ]
}
```

<br />


**Usage** : `$var1$.employeeDetails.FOR_EACH(~obj1-> ~obj1.SET_PROPERTY("Google","company"))`


Add the property *company:Google* to *var1*.


**Output**:

```
[
  {
    "name": "Alex",
    "company": "Google"
  },
  {
    "name": "Bola",
    "company": "Google"
  },
  {
    "name": "Charlie",
    "company": "Google"
  }
]
```

<br />

## Example 3: Given a JSON array without schema, add a property to all the nested JSON objects.


**Sample data**:

```
  $var1$ =
{
  "employeeDetails": [
    {
      "name": "Alex",
      "details": [
        {
          "age": "27",
          "address": "1800 Amphibious Blvd. Mountain View, CA 94045"
        }
      ]
    },
    {
      "name": "Bob",
      "details": [
        {
          "age": "25",
          "address": "8 Rue du Nom Fictif 341 Paris"
        }
      ]
    }
  ],
  "deptDetails": [
    {
      "id1": "HR"
    },
    {
      "id2": "Finance"
    },
    {
      "id3": "Sales"
    }
  ]
}
```

<br />


**Usage** : `$var1$.employeeDetails.FOR_EACH(~obj1-> ~obj1.GET_PROPERTY("details").FOR_EACH(~obj2-> ~obj2.SET_PROPERTY("dd/mm/yyyy", "dob")))`


Add the placeholder property *dob: "dd/mm/yyyy"* to *details* in *var1*.


**Output**:

```
  [
    [
      {
        "age": "27",
        "address": "1800 Amphibious Blvd. Mountain View, CA 94045",
        "dob": "dd/mm/yyyy"
      }
    ],
    [
      {
        "age": "25",
        "address": "8 Rue du Nom Fictif 341 Paris",
        "dob": "dd/mm/yyyy"
      }
    ]
  ]
```

<br />

## Example 4: Given a JSON array with schema, concat the nested properties of the JSON object.


**Sample data**:

```
      $var1$ =
      {"citynames": [
      {
        "city": "Abbeville",
        "info": {
                "pincode": 29620,
                "state": "South Carolina",
                "location" : {
                      "lat" : "50.1055 N",
                     "lon": "1.8368 E"  
                }     
         }    
      },
      {
        "city": "Aberdeen",
        "info": {
                "pincode": AB10,
                "state": "Scotland",
                "location" : {
                      "lat" : "57.1499 N",
                     "lon": "2.0938 W"  
                }     
         }    
      },
      {
        "city": "Benicia",
  "info": {
                "pincode": 94510,
                "state": "California",
                "location" : {
                      "lat" : "38.0494 N",
                     "lon": "122.1586 W"  
                }     
         } 
      }
    ]
  }
```

<br />


**Usage** : `$var1$.citynames.FOR_EACH(~obj1-> ~obj1.city.CONCAT(",").CONCAT(~obj1.info.location.lat).CONCAT(",").CONCAT(~obj1.info.location.lon))`


Concat the nested properties of *city* using a separator (,) in *var1*.


**Output**:

```
[
  "Abbeville,50.1055 N,1.8368 E",
  "Aberdeen,57.1499 N,2.0938 W",
  "Benicia,38.0494 N,122.1586 W"
]
```

<br />

## Example 5: Resolve a JSON array object reference in a template JSON.


**Sample data**:

```
      $var2$ =
      {
        "cityName": "$~obj1.city$",
        "latitude": "$~obj1.info.location.lat$",
        "longitude": "$~obj1.info.location.lon$"
      }
    
```

```
      $var1$ =
      {"citynames": [
      {
        "city": "Abbeville",
        "info": {
                "pincode": 29620,
                "state": "South Carolina",
                "location" : {
                      "lat" : "50.1055 N",
                     "lon": "1.8368 E"  
                }     
         }    
      },
      {
        "city": "Aberdeen",
        "info": {
                "pincode": AB10,
                "state": "Scotland",
                "location" : {
                      "lat" : "57.1499 N",
                     "lon": "2.0938 W"  
                }     
         }    
      },
      {
        "city": "Benicia",
  "info": {
                "pincode": 94510,
                "state": "California",
                "location" : {
                      "lat" : "38.0494 N",
                     "lon": "122.1586 W"  
                }     
         } 
      }
    ]
  }
  
```

<br />


**Usage** : `$var1$.citynames.FOR_EACH(~obj1-> $var2$.RESOLVETEMPLATE())`


Resolves references of `~obj1` in *var2*, where `~obj1` is the current iterating element of *var1*.


**Output**:

```
[
  {
    "cityName": "Abbeville",
    "latitude": "50.1055 N",
    "longitude": "1.8368 E",
  }
  {
    "cityName": "Aberdeen",
    "latitude": "57.1499 N",
    "longitude": "2.0938 W",
  }
  {
    "cityName": "Benicia",
    "latitude": "38.0494 N",
    "longitude": "122.1586 W",
  }
    
]
```

<br />

## Recommendation

- Learn how to add and configure a [Data Mapping task](https://docs.cloud.google.com/application-integration/docs/configure-data-mapping-task.md.txt)