Home > OS >  Need help on Groovy Script
Need help on Groovy Script

Time:02-04

I am trying to remove double quotes from the Value of 'version' but, not able to do that with the following groovy codes. Please help me resolving this.

  • Source JSON:

    {"version":"1", "code":'', "eccQuoteExternalQuoteId":'100000136', "reasonForRejection":'', "rejectionCode":'', "state":{"code":'SOX_APP',"integrationKey":'Approve'}, "integrationKey":''}

Groovy Script:

def Message processData(Message message) {

//Body 
def body = message.getBody(String.class);

def jsonSlurper = new JsonSlurper()
def list = jsonSlurper.parseText(body)

list.each{
    it.version=Integer.parseInt(it.get("version").toString().replace(" ",""));        
    }
def jsonOP = JsonOutput.toJson(list)
 message.setBody(jsonOP)
 return message;

}

CodePudding user response:

Replace

it.version=Integer.parseInt(it.get("version").toString().replace(" ",""));

with

it.version=it.get("version").toString().toInteger();

see the ref toInteger.

CodePudding user response:

Your json is incorrect because there are single quotes used for some values. However you could try LAX parser to ignore this issue.

def jsonSlurper = new JsonSlurper().setType(JsonParserType.LAX)

If the json from question is coming through the message body then just change

list.each{
    it.version=Integer.parseInt(it.get("version").toString().replace(" ",""));        
    }

To

list.version = list.version as Integer
  •  Tags:  
  • Related