Reading time: 6 minutes<\/em><\/span><\/p>\n \n<\/div>\n Recently I had a chance to review a book written by Eng Swee Yeoh<\/a> and Vadim Klimov<\/a>: Developing Groovy Scripts for SAP Cloud Platform Integration<\/strong>.<\/a>\u00a0 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.<\/p>\n 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.<\/p>\n 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.<\/p>\n 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.<\/a><\/p>\n 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:<\/p>\n <\/a><\/p>\n Please find JSON input data that I will use for my scenario:<\/p>\n I created a very simple iflow, which main part is Groovy Script step: JSON2JSON transformation as shown below.<\/p>\n <\/a><\/p>\n 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<\/a> excellent blog written by SAP.<\/p>\n <\/p>\n 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:<\/p>\n The input JSON message will be accessed by Reader and then parsed by previously mentioned JsonSlurper as shown in the code snippet below.<\/p>\n Next, we will count the total number of transactions in number<\/em> and the total number of Card<\/em> transactions in numberCard.<\/em> In order to count total number of transactions made by the Credit Card findAll<\/em> method is used to filter only the transactions with PaymentType<\/em> equal to Card<\/em>. We also define the sum that is the sum of all prices in list Transactions.\u00a0<\/em><\/p>\n Next, the builder will use the JSONBuilder to generate output JSON format. First, we will build the header with Shop<\/em> and CreationDate<\/em>. Next, the TrCardSummary<\/em> node with ItemsNo<\/em>, CardItemsNo<\/em> and TotalAmount<\/em> will be used.<\/p>\n 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.<\/p>\n <\/p>\n 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.<\/p>\n <\/p>\n <\/a><\/p>\n 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.<\/p>\n <\/a><\/p>\n 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.<\/p>\n 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.<\/p>\n 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<\/a>.<\/strong> Apart from transformations from JSON2JON or XML2XML using Groovy you will learn also from the book:<\/p>\n <\/p>\n Please let me in comments if you are currently using Groovy Script in SAP CPI and for which scenarios do you use it?<\/p>\n","protected":false},"excerpt":{"rendered":" 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.\u00a0 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 […]<\/p>\n","protected":false},"author":10,"featured_media":6132,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":""},"categories":[1],"tags":[204,203,74],"acf":[],"yoast_head":"\nIntroduction<\/h3>\n
Transform JSON2JSON with Groovy Script<\/h3>\n
Understanding our scenario<\/h4>\n
\n
\n{\r\n\r\n\u00a0\u00a0\"ShoppingLog\": {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\"Header\": {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"ShopName\": \"The best sport equipment\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Date\": \"2020-06-02\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0},\r\n\r\n\u00a0\u00a0\u00a0\u00a0\"Transactions\": [\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"ID\": \"100001\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Article\": \"Dumbbell 15KG\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Price\": 15,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"PaymentType\": \"Cash\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"ID\": \"100002\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Article\": \"Dumbbell 10KG\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Price\": 10,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"PaymentType\": \"Card\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\r\n\u00a0 {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"ID\": \"100003\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Article\": \"Bike AXB 2.0\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Price\": 400,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"PaymentType\": \"Card\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\r\n\u00a0 {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"ID\": \"100004\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Article\": \"Bike AXB 2.0\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Price\": 400,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"PaymentType\": \"Bitcoin\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0]\r\n\r\n\u00a0\u00a0}\r\n\r\n}\r\n\r\n\r\n\r\n\r\nAnd expected JSON output data:\r\n\r\n{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\"Log\": {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Header\": {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Shop\": \"The best sport equipment\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"CreationDate\": \"2020-06-02\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"TrCardSummary\": {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"ItemsNo\": 4,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"CardItemsNo\": 2,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"TotalAmount\": 825.0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n
<\/h4>\n
Parse JSON message<\/h4>\n
\n
\n<\/div><\/li>\n<\/ul>\n
\nReader reader = message.getBody(Reader)\r\ndef input = new JsonSlurper().parse(reader)<\/pre>\n
\n
\ndef transactions = input.ShoppingLog.Transactions.findAll { item -> item.PaymentType == \"Card\" }\r\ndef numberCard = transactions.size()\r\ndef number = input.ShoppingLog.Transactions.size()\r\ndef sum = 0\r\ninput.ShoppingLog.Transactions.Price.each { Price -> sum += Price as Double\u00a0}<\/pre>\n
\n
\ndef builder = new JsonBuilder()\r\nbuilder.Log {\r\n\u00a0 \u00a0 'Header' {\r\n\u00a0 \u00a0 \u00a0 \u00a0 'Shop' input.ShoppingLog.Header.ShopName\r\n\u00a0 \u00a0 \u00a0 \u00a0 'CreationDate' input.ShoppingLog.Header.Date\r\n\u00a0 \u00a0 }\r\n\r\n\u00a0 \u00a0 'TrCardSummary' {\r\n\u00a0 \u00a0 \u00a0 \u00a0 'ItemsNo' number\r\n\u00a0 \u00a0 \u00a0 \u00a0 'CardItemsNo' numberCard\r\n\u00a0 \u00a0 \u00a0 \u00a0 'TotalAmount' sum\r\n\u00a0 \u00a0 }\r\n}<\/pre>\n
\n
\nimport com.sap.gateway.ip.core.customdev.util.Message\r\nimport groovy.json.JsonBuilder\r\nimport groovy.json.JsonSlurper\r\nimport java.time.LocalDate\r\nimport java.time.format.DateTimeFormatter\r\n\r\ndef Message processData(Message message) {\r\n\u00a0 \u00a0 Reader reader = message.getBody(Reader)\r\n\u00a0 \u00a0 def input = new JsonSlurper().parse(reader)\r\n\u00a0 \u00a0 def transactions = input.ShoppingLog.Transactions.findAll { item -> item.PaymentType == \"Card\" }\r\n\u00a0 \u00a0 def numberCard = transactions.size()\r\n\u00a0 \u00a0 def number = input.ShoppingLog.Transactions.size()\r\n\u00a0 \u00a0 def sum = 0\r\n\u00a0 \u00a0 input.ShoppingLog.Transactions.Price.each { Price -> sum += Price as Double }\r\n\r\n\u00a0 \u00a0 def builder = new JsonBuilder()\r\n\u00a0 \u00a0 builder.Log {\r\n\u00a0 \u00a0 \u00a0 \u00a0 'Header' {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'Shop' input.ShoppingLog.Header.ShopName\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'CreationDate' input.ShoppingLog.Header.Date\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 'TrCardSummary' {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'ItemsNo' number\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'CardItemsNo' numberCard\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'TotalAmount' sum\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 }\r\n\u00a0 \u00a0 message.setBody(builder.toPrettyString())\r\n\u00a0 \u00a0 return message<\/pre>\n
Simulation of JSON2JSON transformation in SAP CPI<\/h4>\n
\nSummary of JSON2JSON transformation scenario<\/h2>\n
\n
\n<\/div><\/li>\n<\/ul>\n