Blog > Speed up your json2json transformation in SAP CPI with Groovyscript

Speed up your json2json transformation in SAP CPI with Groovyscript

CPI_Json2json_Groovy_Nowak
Mateusz Nowak SAP Integration Consultant, SAP Press Author
icon__calendar 2020-06-19

In this article, you will learn how to:

  • How to speed up JSON2JSON transformation using Groovy Script in SAP CPI
  • How to parse and generate message structure in JSON format
  • How to find more useful information for using Groovy Script  in SAP CPI

Reading time: 6 minutes

Introduction

Recently I had a chance to review a book written by Eng Swee Yeoh and Vadim Klimov: Developing Groovy Scripts for SAP Cloud Platform Integration.  The first thing that I have to admit is that reading this book was like reading an adventure book filled with a lot of mysteries. I spent a nice time reading the book and I have learned a lot of useful things regarding Groovy Script in SAP CPI. In this article, I want to share with you one of the concepts I have learned from the book – how you can speed up the transformation of JSON2JSON format.

Why I am writing about speeding up the process? As you know, in SAP Cloud Platform Integration you have the possibility to transform the JSON file from one structure to another JSON structure, but for this, you need to use JSON2XML converter and then XML2JSON converter. This unfortunately forces us to perform some additional steps that we could skip using Groovy Script in SAP CPI. In addition to this, you can use Groovy Script instead of graphical mapping which can be insufficient in very complex transformations.

Transform JSON2JSON with Groovy Script

Understanding our scenario

Let’s imagine that we have a scenario, where we want in SAP CPI transform the JSON format that contains all the shop transactions from specific day to JSON format, that contains only the total number of transactions, the total number of transactions made by credit card (for some statistical purpose) and the total cash flow in that particular day.

As shown below for this purpose we will use almost the same data that we used in my other blog SAP CPI – Combine in Sequence vs Combine Aggregation Algorithm.

In input JSON message to SAP CPI, you will send ShopName and Date. Then in the Transactions node, you will send ID, Article, Price, and Payment Type. Based on the data sent in input file after our transformation in Groovy Script we expect to have Shop and CreationDate in the header level and then ItemsNo (total number all of the transactions), CardItemsNo (total number of transactions made by credit card) and TotalAmount (made by all Payment Types). Our sample input message and output for this message can look like shown below:

Input JSON and Output JSon file for SAP CPI transformation


Please find JSON input data that I will use for my scenario:


Parse JSON message

I created a very simple iflow, which main part is Groovy Script step: JSON2JSON transformation as shown below.

JSON2JSON transformation step in SAP CPI

For the purpose of this blog, we don’t need to define any specific connection details, as all the steps can be simulated using Simulation Mode in SAP CPI. If you are not familiar with Simulation Mode, please check this excellent blog written by SAP.

 

In order to transform JSON2JSON in SAP CPI using Groovy Script as a first step, you need to deserialize data from JSON to Groovy Script objects. Only then you will be able to manipulate the data in Groovy. Fortunately, Groovy does all the work for us, because it provides ready and appropriate classes for this purpose:

  • groovy.json.JsonSlurper that contains parse method and allows us to parse the input message in JSON format
  • groovy.json.JsonBuilder allows us to generate really complex JSON documents that will be necessary to generate our output message.

The input JSON message will be accessed by Reader and then parsed by previously mentioned JsonSlurper as shown in the code snippet below.



Next, we will count the total number of transactions in number and the total number of Card transactions in numberCard. In order to count total number of transactions made by the Credit Card findAll method is used to filter only the transactions with PaymentType equal to Card. We also define the sum that is the sum of all prices in list Transactions. 



Next, the builder will use the JSONBuilder to generate output JSON format. First, we will build the header with Shop and CreationDate. Next, the TrCardSummary node with ItemsNo, CardItemsNo and TotalAmount will be used.



You can find the whole code snippet explained above in one place below. You can just copy and paste it to your Groovy Script step in SAP CPI iflow.


 

Simulation of JSON2JSON transformation in SAP CPI

If you want to test your scenario you can paste the input data I provided at the beginning of the article to the Body section in Simulation Input Step.

 

SAP CPI Simulation Mode - Input step

Then after running the simulation you should get exactly the same output as I also provided at the beginning of the article and as shown below.

SAP CPI successful json2json transformation step


Ideally, after transformation, you should see, that the total number of transactions from the input file is equal to 4 and that only 2 transactions have been recognized as the ones paid by Card. At the and sum of all transactions should be equal to 825.0.

Summary of JSON2JSON transformation scenario

I hope that you find this method very useful and easy to transform JSON input to the JSON output format. As you could see, a very simple script with the usage of JSONSlurper and JSONBuilder can help us build very sophisticated transformations.

I was really excited when I have learned it from the book by Eng Swee and Vadim. If you like this blog and you are hungry for more Groovy Script real-life examples in SAP CPI – you have to get acquainted with this book. Apart from transformations from JSON2JON or XML2XML using Groovy you will learn also from the book:

  • The basic Groovy syntax and commands
  • How to configure your development environment
  • How to test your scripts and how to follow the test-driven development methodology
  • How to access SAP CPI internal frameworks
  • And much much more!

 

Please let me in comments if you are currently using Groovy Script in SAP CPI and for which scenarios do you use it?

Mateusz Nowak SAP Integration Consultant, SAP Press Author
SAP Integration consultant since 2016. Mateusz is the author of SAP Press Book: Interface Monitoring and Error Handling with SAP AIF. He's also a trainer in the openSAP course Virtualize and Automate Your SAP Testing Using Int4 IFTT. Matt has been involved in multiple integrational projects as ABAP Developer, AIF lead and consultant. Skilled at a variety of tools and development techniques including ABAP, AIF, PO, BRFplus, IDocs, Adobe Forms, Smartforms, IDocs, ALE, SCPI.