cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to remove fields in JSON after conversion in groovy script

Former Member
0 Likes
15,894

Remove Field in JSON after conversion

{

"element":

[

{"name:lastname:firstname":" ",

"gender":"M",

"country":"US",

"id":"1"},

]

}

My desired JSON message is

{

"element":

[

{"":" ",

"gender":"M",

"country":"US",

"id":"1"},

]

}

i am using this script code:

import com.sap.gateway.ip.core.customdev.util.Message;import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def Message processData(Messagemessage){
    def body =message.getBody(java.lang.String)asString
    def jsonParser =new JsonSlurper()
    def jsonObject = jsonParser.parseText(body)message.setBody(JsonOutput.toJson(jsonObject["name:lastname:firstname"]))

returnmessage;

}


But field is not removing please help me to remove field from JSON Structure

Regards,

Naveen

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor

Hello Naveen,

Below will work.

Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;
import groovy.json.*;

def Message processData(Message message)
 {
	
    def body =message.getBody();
    def jsonSlurper = new JsonSlurper();
    def list = jsonSlurper.parseText(body);    
    list.element.each{
       it.remove('name:lastname:firstname');       
        }
    def jsonOP = JsonOutput.toJson(list)
    message.setBody(jsonOP);
    return message;
}

Input Data:

{
    "element":
    [
        {
            "name:lastname:firstname": " ",
            "gender": "M",
            "country": "US",
            "id": "1"
        },
        {
            "name:lastname:firstname": " ",
            "gender": "M",
            "country": "US",
            "id": "2"
        }
    ]
}

Regards,

Sriprasad Shivaram Bhat

Former Member
0 Likes

Working fine.Thank you so much Sriprasad Shivaram Bhat

former_member738897
Discoverer
0 Likes

Hi Sri,

I'm facing a similar challenge but my requirement is slightly different. I need to completely remove all entries at element[0].

Please refer to the codes below.

Any help would be highly appreciated.

Input:

{
    "element":
    [
        {
            "name:lastname:firstname": " ",
            "gender": "M",
            "country": "US",
            "id": "1"
        },
        {
            "name:lastname:firstname": " ",
            "gender": "M",
            "country": "US",
            "id": "2"
        }
    ]
}

Desired output:

{
    "element":
    [
        {
            "name:lastname:firstname": " ",
            "gender": "M",
            "country": "US",
            "id": "2"
        }
    ]
}
former_member738897
Discoverer
0 Likes

Looks like I just had to try a few more things; it figured out the solution.

For anyone else who might benefit from this in a similar situation:

The remove() function can accept a string value for the name of the object to be removed, like the case demonstrated in Sri's answer above, AND it can also accept integer values for the index of the object to be deleted within a JSON array.

So, in order to achieve the result I wanted, I just had to write the following: element.remove(0);

This will look for the object at index [0] within the JSON array element and delete it.

Answers (1)

Answers (1)

former_member742442
Discoverer
0 Likes

Hello ,

In the above case if we define exchange property with all the field that needs to be removed from the list as comma separated then how do we pass that property in remove function.

Example: Exchange Property

Deletelistvalues {a,b,c,d}

Input:

[a,b,c,d,e,f,g,]

[a,b,c,d,e,f,g,]

[a,b,c,d,e,f,g,]

Expected Output:

[e,f,g,]

[e,f,g,]

[e,f,g,]

Regards,

Shradha