cancel
Showing results for 
Search instead for 
Did you mean: 

Format json body by removing an object using Groovy in CPI

niyatijain2
Explorer
0 Kudos

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>
MortenWittrock
Active Contributor

Hi Nayati

Your JSON is a bit of a mess. Please try to 1) make sure it is valid (it isn't right now) and 2) format it for readability. The same goes for the code. That makes it a lot easier to answer the question.

Also: In your question you state that you want to remove the shippingPackages property, but the code seems to remove everything but the value of the shippingPackages property.

What should the payload look like after your script?

Regards,

Morten

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

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

niyatijain2
Explorer
0 Kudos

Hello Morten,
I tried this code in my original payload and the whole body is gettin passed where it is not considering the 'shippingPackages'. The original payload is a long code and due to copyright, I cannot forward you the true code but if you can help me with this.

Thank you in advance

MortenWittrock
Active Contributor
0 Kudos

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

niyatijain2
Explorer
0 Kudos

Thank you very much,
I had the same field name but it was in another property. Through your easy expanation I found my explanation and it is working now. Again Thanks :

MortenWittrock
Active Contributor
0 Kudos

Hi niyatijain2

Glad to hear it!

Regards,

Morten

Answers (1)

Answers (1)

Nikhil_Gursal
Participant
0 Kudos

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
niyatijain2
Explorer
0 Kudos

Thank you Nikhil,

but my original payload is not much like a simple payload as I have provided as it contains more 10+ properties with more objects inside as well so thank you for providing an answer but this won't help me passing the original body.

Regards,

Niyati Jain