Speed up your json2json transformation in SAP CPI with Groovyscript
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:
Please find JSON input data that I will use for my scenario:
{ "ShoppingLog": { "Header": { "ShopName": "The best sport equipment", "Date": "2020-06-02" }, "Transactions": [ { "ID": "100001", "Article": "Dumbbell 15KG", "Price": 15, "PaymentType": "Cash" }, { "ID": "100002", "Article": "Dumbbell 10KG", "Price": 10, "PaymentType": "Card" }, { "ID": "100003", "Article": "Bike AXB 2.0", "Price": 400, "PaymentType": "Card" }, { "ID": "100004", "Article": "Bike AXB 2.0", "Price": 400, "PaymentType": "Bitcoin" } ] } } And expected JSON output data: { "Log": { "Header": { "Shop": "The best sport equipment", "CreationDate": "2020-06-02" }, "TrCardSummary": { "ItemsNo": 4, "CardItemsNo": 2, "TotalAmount": 825.0 } } }
Parse JSON message
I created a very simple iflow, which main part is Groovy Script step: JSON2JSON transformation as shown below.
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.
Reader reader = message.getBody(Reader) def input = new JsonSlurper().parse(reader)
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.
def transactions = input.ShoppingLog.Transactions.findAll { item -> item.PaymentType == "Card" } def numberCard = transactions.size() def number = input.ShoppingLog.Transactions.size() def sum = 0 input.ShoppingLog.Transactions.Price.each { Price -> sum += Price as Double }
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.
def builder = new JsonBuilder() builder.Log { 'Header' { 'Shop' input.ShoppingLog.Header.ShopName 'CreationDate' input.ShoppingLog.Header.Date } 'TrCardSummary' { 'ItemsNo' number 'CardItemsNo' numberCard 'TotalAmount' sum } }
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.
import com.sap.gateway.ip.core.customdev.util.Message import groovy.json.JsonBuilder import groovy.json.JsonSlurper import java.time.LocalDate import java.time.format.DateTimeFormatter def Message processData(Message message) { Reader reader = message.getBody(Reader) def input = new JsonSlurper().parse(reader) def transactions = input.ShoppingLog.Transactions.findAll { item -> item.PaymentType == "Card" } def numberCard = transactions.size() def number = input.ShoppingLog.Transactions.size() def sum = 0 input.ShoppingLog.Transactions.Price.each { Price -> sum += Price as Double } def builder = new JsonBuilder() builder.Log { 'Header' { 'Shop' input.ShoppingLog.Header.ShopName 'CreationDate' input.ShoppingLog.Header.Date } 'TrCardSummary' { 'ItemsNo' number 'CardItemsNo' numberCard 'TotalAmount' sum } } message.setBody(builder.toPrettyString()) return message
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.
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.
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?
Popular tags
ABAP int4 INT4 IFTT Int4Interview S/4HANA SAP AIF SAP CPI sap integration