cancel
Showing results for 
Search instead for 
Did you mean: 

JSON Structure with field names "JSON" to replace into field names JSON

0 Kudos

Dear Experts,

JSON Structure field having double quotes with field names "JSON" to replace into field names JSON.

E.g:

Input Payload:

{

"structure":"JSON"

}

Output payload should come like this:

{

"Struture": JSON

}

My requirement is to remove the double quotes from target value actually i have tried the below groovy code it is not working
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {
    
    //Body 
    def body = message.getBody(String.class);
    
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)
    
    list.each{
        it.Structure=Integer.parseInt(it.get("Structure").toString().replace(" ",""));        
        }
    def jsonOP = JsonOutput.toJson(list)


    message.setBody(jsonOP)
    return message;
}

Regards,
Naveen

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member757555
Discoverer
0 Kudos

I am getting same Error. DO we have any solution ?

0 Kudos

naveen.arockiaraj hav you got this ans.. please share if yes ?

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Naveen,

Have you checked below thread which might help you.

Converter

Regards,

Sriprasad Shivaram Bhat

0 Kudos

Hi Sriprasad,

Thanks for Replying.

I had gone through that thread i have used this below code for integer value it is working but string value it is not working:

My Requirement is:

Json Structure String to String

Input Payload:

{

"name":"naveen"

}

Output Payload:

{

"name": naveen

}

It is not working getting this error:

java.lang.Exception: java.lang.NumberFormatException: For input string: "naveen"@ line 13 in RemoveQuotes.groovy

as per the code it is working fine

JSON Strcuture String to Integer

{

"name":"1000"

}

Output Payload:

{

"name": 1000

}

but i need to remove that double quotes in char,Whether it is possible Please help me to complete my requirement.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {
    
    //Body 
    def body = message.getBody(String.class);
    
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)
    
     list.Root.each{
        it.name=Integer.parseInt(it.get("name").toString().replace(" ",""));        
        }

    def jsonOP = JsonOutput.toJson(list)

    message.setBody(jsonOP)
    return message;
}