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

SAP Cloud Platform Integration – Groovy script to remove an empty array from an object array

beverely_parks2
Participant
0 Likes
3,858

I'm working on an implementation where the receiver is requesting a JSON file format. I'm using integration mapping to create an XML that matches the JSON format. I'm then executing standard XML to JSON conversion followed by a groovy script to remove the "element" entries from each object array. My final step is that if an object array contains an empty element, the result should be an empty object array that looks like: "TestQualitativeMeasurement":[].

If I create the element regardless of data, convert XML to JSON, and execute my script, my results look like "TestQualitativeMeasurement":[""] (Receiver is not accepting this as an empty object)

I've tried not mapping the element if there is no data but this results in the object array end up looking like "TestQualitativeMeasurement":"" and is missing the [] object qualifers required.

Would anyone be able to advise me on how to accomplish this? I believe that I should be able to do this within my groovy script but I haven't found the right combination.

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

r_herrmann
Active Contributor
0 Likes

Hi Beverly,

since JSON is just a "String", you could simply run a search-replace script after your JSON conversion.

import com.sap.gateway.ip.core.customdev.util.Message
def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String
    body = body.replace("[\"\"]","[]")
    message.setBody(body)
    return message
}

Answers (1)

Answers (1)

beverely_parks2
Participant
0 Likes

Thank you. That worked perfectly and as I try to learn Groovy scripting, I learned about escape characters.