cancel
Showing results for 
Search instead for 
Did you mean: 

JSON string to integer in groovy script removing leading zero's

mayankibm
Explorer
0 Kudos

Hi, 

I have a requirement where in JSON value need to be send as integer instead of string.

Method in groovy script is: 

def json = message.getBody(java.lang.String)
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(json)object.issuer.branchCode = object.issuer.branchCode.toDouble().intValue();

Value is branchCode: "0732340"

Output of above groovy is branchCode: 732340

Expected output is : branchCode: 0732340

Please help with changes that can be done in the code or any alternate code which can be used.

Thanks & Regards,

Mayank

 

 

 

 

 

 

 

View Entire Topic
Edrilan_Berisha
Product and Topic Expert
Product and Topic Expert

Hi,

 

what sachin_yadav3

proposed will not work. I think he is mixing it up. It's not about to remove leading zeros because that would then work already right now. It's about how to maintain the leading zeros. The answer is actually: it's impossible.

Mathematical, there are no leading zeros. The first integer number 1 is "1" it's not "01" because then it would mathematical not clear actually how many "leading zeros" you have. Is 1 maybe "00001" or is it "00000000000001" or .... etc. So integer numbers by definition do not have any leading zeros, ever.

If you want to keep using the leading zeros, you have to get rid of your code line where you convert it to double and parse it to integer:

 

 

 

object.issuer.branchCode.toDouble().intValue();

 

 

 this line of coding will always remove the leading zeros. Get rid of the "toDouble().intValue()". And check if you make use of toString() or if leaving it just like it is isn't already fine enough for you.

 

Best,

Edrilan Berisha

SAP S/4HANA Cloud Financials Development