> ## 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.

# Masked Data Transmission in DB2-API Web Service Response

> Masking sensitive data in web service response coming from DB2-API with Groovy script and transmitting it

## Groovy Script

```groovy theme={null}
import groovy.json.JsonOutput

def parser = new groovy.json.JsonSlurper()
def jsonResp = parser.parseText(responseBodyTextToClient)

static String mask(String input, int exceptFirst, int exceptLast) {
    String regex="(?<=.{" + exceptFirst + "}).(?=.{"+ exceptLast + "})"
    return input.replaceAll(regex, "*")
}

for(Object data in jsonResp.data) {
    data.ADI = mask(data.ADI, 1, 2)
    data.SOYADI = mask(data.SOYADI, 1, 2)
    data.NUMARA = mask(data.NUMARA, 1, 2)
}

responseBodyTextToClient=JsonOutput.prettyPrint(JsonOutput.toJson(jsonResp))
```

## Explanation

This script:

1. **JSON Parse Operation**: Parses the incoming response (`responseBodyTextToClient`) in JSON format.
2. **Mask Function**: Masks the middle characters with `*` while preserving the first and last characters of the given string.
3. **Data Processing**: Loops through the `data` array in JSON and masks the `ADI`, `SOYADI`, and `NUMARA` fields of each element.
4. **Result**: Converts the processed JSON back to string format and returns it as a response.

This approach ensures that sensitive data is transmitted securely.

<Note>
  This script should be run on the response line (Response Policy) because it uses the `responseBodyTextToClient` variable.
</Note>
