SAP CPI Groovy Scripts – Delta Change, Remove Element after the JSON Conversion for single and multiple Nested Array Structure of REST API’s.

This blogs explain about most commonly used groovy scripts when you are working with integration with REST API’s for master data and incremental data delta changes between the source system and target system. We can have a time scheduler in SAP CPI for calling the source system and pull the delta changes records and process to target application.

Business scenario: when we need to call SAP SuccessFactors system with SF ODATA adapter, we can defined first step as a groovy script in SAP CPI iflow and pass the Current Date -1 for yesterday modified or created data synch / or pass the Current date -7 for weekly delta/incremental changes replication between SAP SF and Target REST API application system.

Groovy Script:


def Message processData(Message message) {

def yesterday = new Date() – 1   // if you pass new Date -7 it will bring last 7 days changes from source system.

message.setProperty(“yesterday”, yesterday.format(“yyyy-MM-dd’T’HH:mm:ss.000”))

return message


//previous day  code if you change day -7 it will bring all the recent 7days delta changes for data synch between source and target applications.

When target REST API application designed for accepting the multiple records in array main or nested sub structure we can use the below groovy code for removing the element tag and process the JSON format data in required format.

JSON Nested Array – Structure sample.


import java.util.HashMap;

import groovy.json.JsonSlurper;

import groovy.json.JsonOutput;

import groovy.json.*;

def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String;

def jsonSlurper = new JsonSlurper();

def list = jsonSlurper.parseText(body);

list.customFields = list.customFields.element


list.scorecard.sections.element.parameters.eachWithIndex {


idx ->

list.scorecard.sections.element.parameters[idx]= list.scorecard.sections.element.parameters.element[idx]


//list.scorecard.sections = list.scorecard.sections.element

list.scorecard.sections = [list.scorecard.sections.element]

// the above line array defined with square brackets when you require array structure in target JSON file however this also can be achieved with xml to json coverstion with streaming option – check.

def jsonOP = JsonOutput.toJson(list)


return message;


Removing Element tag after the JSON conversion – when we have single array structure in REST API when we want to remove the element tag from the first line we can use this below groovy.


import groovy.json.JsonSlurper

import groovy.json.JsonOutput

def Message processData(Message message) {

def body = message.getBody(java.lang.String) as String

def jsonParser = new JsonSlurper()

def jsonObject = jsonParser.parseText(body)


return message;



Hope this blog and groovy script helps!!.


Krushi Nenavath – Accenture.