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
Request clarification before answering.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.