Skip to Content
0
Jan 22, 2023 at 10:26 AM

Conversion of some Specific JSON fields into Integers using groovy

641 Views

Hi Everyone,

As per my requirement I need to send Json structure to my target system. For this I used an XML to JSON converter but After converting every Json filed has double quotes(String).

{

"generalInvoiceInfo": {

"invoiceType": "1",

"templateCode": "12/46",

"invoiceSeries": "ABC234",

"invoiceIssuedDate": "2022/12/06",

"currencyCode": "XYZ",

"adjustmentType": "1",

"adjustmentInvoiceType": "1",

"paymentStatus": "true",

"cusGetInvoiceRight": "true",

"exchangeRate": "",

"transactionUuid": "123456789",

"userName": "ABNC_XYZ"

},

}

But as per my user requirement I need some specific fields to integer .

{

"generalInvoiceInfo": {

"invoiceType": "1",

"templateCode": "12/46",

"invoiceSeries": "ABC234",

"invoiceIssuedDate": "2022/12/06",

"currencyCode": "XYZ",

"adjustmentType": "1",

"adjustmentInvoiceType": "1",

"paymentStatus": "true",

"cusGetInvoiceRight": "true",

"exchangeRate": "",

"transactionUuid": "123456789",

"userName": "ABNC_XYZ"

},

}

I wrote the below code and it is not giving any error while testing but my required Fields are not converted to Integer.

This is the script I used:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import groovy.json.*

def Message processData(Message message)

{

def jsonbody = message.getBody(java.lang.String) as String;

def jsonSlurper = new JsonSlurper();

def list = jsonSlurper.parseText(jsonbody);

list.generalInvoiceInfo.record.each

{

it.invoiceType=Integer.parseInt(it.get("invoiceType").toString());

it.adjustmentType=Integer.parseInt(it.get("adjustmentType").toString());

it.adjustmentInvoiceType=Integer.parseInt(it.get("adjustmentInvoiceType").toString());

}

def jsonOP = JsonOutput.toJson(list);

message.setBody(jsonOP);

return message;

}

Please help if there is anything wrong in fetching values or returning output and help me to achieve this.