Skip to Content
0
Jun 22, 2022 at 11:18 AM

SAP Cloud Integration Groovy script to convert JSON string to integer. json two array

604 Views

SAP Cloud Integration Groovy script to convert JSON string to integer , input json contains two array,

I have tried many codes from scn blogs but its not woring on input json which contains two array.

Input :-

{

"merchant_id":"114",

"update_product_request":[

{"store_id":"Y029",

"update_product_request_list":[

{

"quantity":"1",

"original_price":"200",

"sale_price":"200",

"uom":"EA",

"currency":"",

},

{

"quantity":"5",

"original_price":"100",

"sale_price":"100",

"uom":"EA",

"currency":"",

}]}]}

Groovy :-

import com.sap.gateway.ip.core.customdev.util.Messageimport groovy.json.JsonSlurperimport groovy.json.JsonOutput
def Message processData(Message message) { def json = message.getBody(java.lang.String) def jsonSlurper = new JsonSlurper() def object = jsonSlurper.parseText(json)
object.update_product_request.update_product_request_list.each { item -> if (item.quantity != null && !item.quantity.isEmpty()) { item.quantity = item.quantity.toInteger().intValue() }else { item.quantity = 0.toInteger().intValue() } if (item.original_price != null && !item.original_price.isEmpty()) { item.original_price = item.original_price.toDouble().intValue() }else {item.original_price = 0.toDouble().intValue()} if (item.sale_price != null && !item.sale_price.isEmpty()) { item.sale_price = item.sale_price.toDouble().intValue() }else {item.sale_price = 0.toDouble().intValue()} } message.setBody(JsonOutput.toJson(object)) return message}