on ‎2018 Sep 24 11:20 AM
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
Request clarification before answering.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Working fine.Thank you so much Sriprasad Shivaram Bhat
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"
}
]
}
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 7 | |
| 7 | |
| 6 | |
| 6 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 3 | |
| 3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.