> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Simple Map and Foreach Loop Usage

> Processing JSON data using map and foreach loops with Groovy script and copying to headers

## Processing Data with For Loop

Processes JSON data coming from client by iterating through the data. In this example, it is assumed that the incoming data has a personnel information list.

```groovy theme={null}
def parser = new groovy.json.JsonSlurper()
def jsonData = parser.parseText(requestBodyTextFromClient)

static String someRandomMethod(String input, int inputNumber) {
    //Operations..
    return input
}

for(Object data in jsonData.data) {
  data.name = someRandomMethod(data.name, 1)
  data.surname = someRandomMethod(data.surname, 3)
  data.phoneNumber = someRandomMethod(data.phoneNumber, 5)
}
```

## Copying to Header with Each Method

Copies JSON data coming from client to the header section of the message and sends it to the backend. If there are sub-objects or lists, they are added as-is.

```groovy theme={null}
def parser = new groovy.json.JsonSlurper()
def jsonData = parser.parseText(requestBodyTextFromClient)

jsonData.each { k, v ->    
      requestHeaderMapToTargetAPI.put(k,v)
}
```

## Sequential Processing with EachWithIndex

Processes JSON data coming from client by retrieving each value with its order information for use in the loop.

<Note>
  These script examples should be run on the request line (Request Policy) because they use the `requestBodyTextFromClient` and `requestHeaderMapToTargetAPI` variables.
</Note>

```groovy theme={null}
def parser = new groovy.json.JsonSlurper();
def jsonData = parser.parseText(requestBodyTextFromClient);

static String someRandomMethod(String input, int inputNumber) {
    //Operations..
    return input
}

jsonData.eachWithIndex { val, idx -> 
   val = someRandomMethod(val, idx)
}
```
