Skip to main content

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.
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.
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.
These script examples should be run on the request line (Request Policy) because they use the requestBodyTextFromClient and requestHeaderMapToTargetAPI variables.
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)
}