Hi EveryOne,
I have a requirement to convert the string data type to integer data type if the field has an integer number, else if the field has a string value pass it as it is.
for example, I have the payload

for the field "PurReqnSSPCatalogForEdit" I have an integer value. I need to convert it from string to integer.
I have written the below groovy script for this
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
def Message processData(Message message) {
//Body
def jsonOP = message.getBody(String.class);
jsonOP=jsonOP.toString()
def json_to_str="{\"Element\":["+jsonOP+"]}"
def jsonSlurper = new JsonSlurper();
def list = jsonSlurper.parseText(json_to_str);
list.Element.each{
it.EmployeeForEdit.toString()
it.BusinessObjectTypeForEdit.toString()
it.PurReqnSSPCatalogForEdit=Integer.parseInt(it.get("PurReqnSSPCatalogForEdit").toString());
}
def jsonlist = JsonOutput.toJson(list);
def jsonObject = jsonSlurper.parseText(jsonlist);
def removerootnode = JsonOutput.toJson(jsonObject["Element"]);
def removebraket = removerootnode.replace("[","");
def removeendbraket = removebraket.replace("]","");
message.setBody(removeendbraket);
return message;
}
But if the field "PurReqnSSPCatalogForEdit" has a string value parse it as it is. What is the condition I need to add to the above groovy script to achieve this? Please provide a solution for it
Thank You,
Vineetha.
Request clarification before answering.
Hi Vineetha,
Modify the below code as per your requirement.
try{
if (val.isInteger() == true)
{
def modifedVal = val.toInteger()
}
}
catch(Exception ex) {
}
Here val is the field name.
Regards,
Priyanka
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Vineetha,
Pleaes refer below KBA.
https://userapps.support.sap.com/sap/support/knowledge/en/2521044
https://answers.sap.com/questions/168650/sap-hci-xml-to-json-converter-string-to-integer.html
Regards,
Sriprasad Shivaram Bhat
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 10 | |
| 5 | |
| 5 | |
| 5 | |
| 4 | |
| 2 | |
| 2 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.