cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

groovy script to get data json

former_member76287
Participant
0 Likes
5,539

Dear Expert,

I need your advise

how can i get the data from this json ?

{
"status": "success",
"code": "SSR001",
"message": "Order Exist",
"order_number": "2817183923949",
"order_date": "2018-12-28 10:29:03",
"delivery_method": "Link",
"data": [
{
"id_voucher": "IDTEST119",
"kode_voucher": "S0RWMTE5",
"tgl_expired": "2019-12-31",
"url": "https://cxxx.me/f39b9a51250556be8924b85da9558e6b/1537176870"
},
{
"id_voucher": "IDTEST334",
"kode_voucher": "S0RWMzM0",
"tgl_expired": "2019-12-31",
"url": " https://cxxx.me/g39b9a31250556be8924b85da9558e6b/1537176871";
}
],
"current_page": 1,
"total_page": 1,
"maximal_record": 10,
"total_record": 2
}

do you have idea ?

if have tried with this code but i'm feel lost

/*
 The integration developer needs to create the method processData 
 This method takes Message object of package com.sap.gateway.ip.core.customdev.util 
which includes helper methods useful for the content developer:
The methods available are:
    public java.lang.Object getBody()
	public void setBody(java.lang.Object exchangeBody)
    public java.util.Map<java.lang.String,java.lang.Object> getHeaders()
    public void setHeaders(java.util.Map<java.lang.String,java.lang.Object> exchangeHeaders)
    public void setHeader(java.lang.String name, java.lang.Object value)
    public java.util.Map<java.lang.String,java.lang.Object> getProperties()
    public void setProperties(java.util.Map<java.lang.String,java.lang.Object> exchangeProperties) 
    public void setProperty(java.lang.String name, java.lang.Object value)
    public java.util.List<com.sap.gateway.ip.core.customdev.util.SoapHeader> getSoapHeaders()
    public void setSoapHeaders(java.util.List<com.sap.gateway.ip.core.customdev.util.SoapHeader> soapHeaders) 
       public void clearSoapHeaders()
 */
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*


def Message processData(Message message) {
    //Properties
    def map = message.getProperties()
    def effectiveStartDate = map.get("effectiveStartDate")
    def externalCode = map.get("externalCode")
    def transactionSequence = map.get("transactionSequence")
    
    //Body 
    def body = message.getBody(String.class)
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)
    def status = list.status.toString()
    def kode_voucher = list.kode_voucher.toString()
   
    
    body = [cust_TransactionWallet2: [
    effectiveStartDate: effectiveStartDate,
    externalCode: externalCode,
    transactionSequence: transactionSequence,
    cust_status: status,
    cust_notes: kode_voucher,
    ]]
 
    def json_str = JsonOutput.toJson(body)


    message.setBody(json_str);
    
     
    	
    return message;
}
View Entire Topic
Sriprasadsbhat
Active Contributor
0 Likes

Hello Achmad,

Below will do the needful.

 def Message processData(Message message)
 {
    def Output="";
    //Body 
    def body = message.getBody(String);
	
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body as String)
    
    list.data.each
    {
     Output=Output+"url: "+it.url+"\n"
    }
    
    message.setBody(Output.toString())
    return message;             
 }

Regards,

Sriprasad Shivaram Bhat

former_member76287
Participant
0 Likes

Hi sriprasadshivaramabhat

Is there any way i can find about this script ?

i really didnt know if that script have a way on that part .

please advise

Yogananda
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi sriprasadshivaramabhat

In your code, "it." it should be "list." right ? Am i correct ?

Output=Output+"url: "+it.url+"\n"
0 Likes

Hi Sriprasad Sir,

can you please suggest how we can read the json data field and set as a header or property.