Skip to Content
Technical Articles
Author's profile photo SAKRIYANAIK NK

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

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.

Delta Change – Incremental Data Replication Between source and target systems.

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

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

return message



//previous day  code


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


Remove Element – Remove the Element Header and Nested Array  Tag after the XML to JSON Conversion.



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;



When you are calling the multiple entities from source system and combining with multi mapping with help of below groovy script we can remove the multi mapping tags from required output format.

import java.util.HashMap;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;

body = body.replaceAll(“<ns0:Messages xmlns:ns0=”, “”);

// 2ND TAG —
body = body.replaceAll(“\”://\”>”, “”); // http

body = body.replaceAll(“<ns0:Message1>”, ” “);

// closing 1st tag —
body = body.replaceAll(“</ns0:Messages>”, “”);

body = body.replaceAll(“</ns0:Message1>”, ” “);

//Closing 4th Tag is Root

// body = body.replaceAll(“</root>”, “”);

return message;



Hope this blog and groovy script helps!!.



Krushi Nenavath – Accenture.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Daniel Graversen
      Daniel Graversen

      Hi Krushi,


      It is possible to calculate date +/1 in camel expression so you can just use a Content Modifier

      See my comment.

      Author's profile photo SF Support2
      SF Support2

      Hi Daniel,

      we do not require to pass future date as there will not any changes in source system with future date!.  However, if we schedule a job in SAP CPI today @ 6PM it will fetch all yesterday's and todays records till 6pm. the receiver system should have provision to update the existing records and create new one's!!.

      def yesterday = new Date() – 1 


      Krushi Nenavath

      Author's profile photo Alexander Aigner
      Alexander Aigner

      Hi Krushi,

      you can also do this for the past. Example:


      I think this is what Daniel ment.

      Best regards,

      Author's profile photo KESAVA SUGALI

      Hi Krushi, Great blog.