Adding New Field to REST Body Using JsonSlurper
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:
- JSON Parse: JSON message coming from request body is parsed
- Field Addition: A new field (
tamAd) is added using existing fields - JSON Creation: Updated JSON message is created
not
This script should be run on the request line (Request Policy) because it uses the requestBodyTextFromClient and requestBodyTextToTargetAPI variables.