cancel
Showing results for 
Search instead for 
Did you mean: 
Subscribe

Hello Experts,

I need to use Groovy script to overcome the shortcomings of the standard XML to JSON converter on the Cloud Platform Integration.

Here is the output JSON format I need to get:

{
"PO": "test21",
"Items": [{
"Id": "000010",
"Product": "XXXXX",
"Quantity": 5,
"Discount_Percent": 0,
"Unit_Price": 0,
"Pharmaceutical": false,
}]
}

But this is what I received out of the converter:

{
"PO": "test21",
"Items": [{
"Id": "000010",
"Product": "XXXXX",
"Quantity": "5.000",
"Discount_Percent": "0.000",
"Unit_Price": "",
"Pharmaceutical": "false",
}]
}

I'm not a Groovy expert and tried utilizing the one provided in the SAP note to replace "false" with false using the following groovy but that didn't work:

import com.sap.gateway.ip.core.customdev.util.Message;

def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
String output = body.replaceAll("\"false\"", "\$1");
message.setBody(output);
return message;
}

Can someone please help with a groovy script that will work for me?

Thanks for your time.

Anirban

View Entire Topic
ibibhu
Product and Topic Expert
Product and Topic Expert
0 Likes

HI,

Please check your regular expression and change it as given below :-

String output = body.replaceAll("\"(false)\"", "\$1");

"( )" => Groups multiple tokens together and creates a capture group for extracting a sub string.

I tried the same but i didn't get any error.

Input : -

Output:


As already shared by 7a519509aed84a2c9e6f627841825b5a, that's the ideal way to typecast a given string to respective type.

Regards,
Bibhu

amallick
Participant
0 Likes

Hi Bibhu,

I received the same error as before:

Here is the updated code:

import com.sap.gateway.ip.core.customdev.util.Message;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
String output = body.replaceAll("\"(false)\"", "\$1");
message.setBody(output);
return message;
}

Thank you.

amallick
Participant
0 Likes

Thank you, Bibhu - your answer is a valid one and it worked for me after I removed the "Ccript Function" as suggested by Morten, but unfortunately I cannot accept 2 answers.

Thanks again for your time and support.

Regards,

Anirban