Skip to main content

Groovy Script

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def jsonSlurper = new JsonSlurper()
def jsonMessage = jsonSlurper.parseText(requestBodyTextFromClient)

jsonMessage.put("tamAd", jsonMessage.ad + " Api Management")

requestBodyTextToTargetAPI=JsonOutput.toJson(jsonMessage)

Example

Original Request

{
  "ad": "Apinizer"
}

New Request

{
  "ad": "Apinizer",
  "tamAd": "Apinizer Api Management"
}

Explanation

This script performs the following operations:
  1. JSON Parse: JSON message coming from request body is parsed
  2. Field Addition: A new field (tamAd) is added using existing fields
  3. JSON Creation: Updated JSON message is created
This script should be run on the request line (Request Policy) because it uses the requestBodyTextFromClient and requestBodyTextToTargetAPI variables.