on 12-01-2023 6:36 AM
Hello all,
I am having a json body which I need to format where there's an object called 'shipping_packages' which I need to remove but when I use the below code, I get null in the result.. Can anyone help me with this?
Input:
{
"abxy": [
{
"abc": "123"
}
],
"shippingPackages": [
{
"def": "234",
"hij": "456",
"klm": {
"nop": "678"
}
}
]
}<br>
and my desired output is:
{
"abxy": [
{
"abc": "123"
}
]
}<br>
and the code I am trying for which I am getting null is:
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.time.TimeCategory import java.util.HashMap;
import groovy.json.JsonSlurper;
import groovy.json.JsonBuilder def Message processData(Message message) {
def body=message.getBody(String);
def map=new JsonSlurper();
def object=map.parseText(body) def builder=new JsonBuilder(object.shippingPackages) message.setBody(builder.toString()) return message;
}<br>
Hi Nayati
Thanks for the nicely formatted JSON and code!
Here is a script that removes the shippingPackages property:
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
def Message processData(Message message) {
// Parse the JSON payload.
def payload = message.getBody(Reader)
def json = new JsonSlurper().parse(payload)
// Remove the 'shippingPackages' property.
json.remove('shippingPackages')
// Convert back to a string.
def updatedJson = JsonOutput.toJson(json)
// Set the updated JSON payload back to the message.
message.setBody(updatedJson)
return message
}
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi niyatijain2
Well, the code will - of course! - not work for a different payload. This should be clear to you. Fortunately, everything you need is in the script I gave you. This line:
json.remove('shippingPackages')
should be changed to navigate to the correct place in the JSON object. If the payload looked like this:
{
"a": {
"b": {
"c": {
"message": "Hi!"
}
}
}
}
and you wanted to remove the "c" property, you would do this:
json.a.b.remove('c')
Now the rest is up to you, so please study the script and make the required change. That way you will also understand the code better, and you really shouldn't add any code to your iflow that you do not understand.
Regards,
Morten
Hi niyatijain2 ,
below groovy script will help you achieve target Json as per your requirement.
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def Message processData(Message message) {
def body = message.getBody(String)
def jsonSlurper = new JsonSlurper()
def jsonObject = jsonSlurper.parseText(body)
// Keep only "abxy" data
def abxyData = jsonObject.abxy
// Create a new JSON object with only "abxy" data
def newJsonObject = [abxy: abxyData]
// Convert the new JSON object to a JSON string
def jsonBuilder = new JsonBuilder(newJsonObject)
def jsonString = jsonBuilder.toString()
// Set the modified JSON string as the new message body
message.setBody(jsonString)
return message
}
Regards,Nikhil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
10 | |
9 | |
8 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.