cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Cloud Integration Groovy script to convert JSON string to integer

beverely_parks2
Participant
0 Kudos

I have a requirement to send a JSON file to a trading partner. As a part of this requirement, there is one field that needs to be converted from a string to an integer. I have that working when the field is populated, but most of the time, this field will not be populated so it will not exist in the JSON file. I have tried to follow this blog: http://saphcidemo.blogspot.com/2017/10/json-code-for-conversion-of-string-to.html . The issue that I'm currently having is that when I add the if(jsonDataObject.assignedTo!= null) statement to my script, I'm receiving the error: java.lang.NullPointerException: while trying to invoke the method com.sap.gateway.ip.core.customdev.util.Message.getBody() of a null object loaded from local variable 'result'

This is the script that I'm working with:

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

import groovy.json.*

def Message processData(Message message)

{

def jsonbody = message.getBody(java.lang.String) as String;

def jsonSlurper = new JsonSlurper();

def jsonDataObject = jsonSlurper.parseText(jsonbody);

//Convert the JSON String to Int

//if (jsonDataObject.check_transaction_code!= null){

jsonDataObject.check_transaction_code = convertToInt(jsonDataObject.check_transaction_code);

//Integer conversion

message.setBody(new JsonBuilder(jsonDataObject).toString());

return message;

//}

}

static Object convertToInt(Object inputValue){

if(inputValue.isNumber()){

return inputValue.toInteger();

}else{

return inputValue;

}

}

Would anyone be able to advise me where I'm going wrong?

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Beverly,

Please find the below thread which might resolve your issue.

https://answers.sap.com/questions/168650/sap-hci-xml-to-json-converter-string-to-integer.html

Regards,

Sriprasad Shivaram Bhat

beverely_parks2
Participant
0 Kudos

I'm not sure that it does. My issue is that if the field isn't contained in the JSON stream, I'm receiving an exception. I've tried mulitple combinations of if statements, but I can't seem to find the correct one for determining if the field is exists.

Answers (4)

Answers (4)

Sriprasadsbhat
Active Contributor

Hello Beverly,

Below will work for you

Input Data:

{
    "EmployeeData": {
        "Record": [{
                "PersonID": "P11",
                "UserID": "31",
                "EmployementID": "E221"
            }, {
                "PersonID": "P12",
                "UserID": "32",
                "EmployementID": "E222"
            }, {
                "PersonID": "P13",
                "UserID": "33",
                "EmployementID": "E223"
            }
        ]
    }
}

Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;

def Message processData(Message message) {    
    //Body 
    def body = message.getBody(String.class);    
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)
    
    list.EmployeeData.Record.each
   {       
       it.PersonID.toString()
       if ( it.UserID != "")
       {
         it.UserID=Integer.parseInt(it.get("UserID").toString());
       }
       else 
       {
         it.UserID=it.UserID
       }
        it.EmployementID.toString()
    }    
    def jsonOP = JsonOutput.toJson(list)
    message.setBody(jsonOP)
    
return message;
}

Output Data:

{
	"EmployeeData": {
		"Record": [{
				"PersonID": "P11",
				"UserID": 31,
				"EmployementID": "E221"
			}, {
				"PersonID": "P12",
				"UserID": "",
				"EmployementID": "E222"
			}, {
				"PersonID": "P13",
				"UserID": 33,
				"EmployementID": "E223"
			}
		]
	}
}

Hope this helps!

Regards,

Sriprasad Shivaram Bhat

0 Kudos
{
    "root": [{
        "teams": [
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "422511",
                "complete": "false"
            },
            {
                "VisitFrequencyPerYear": "0",
                "UPI": "421802",
                "complete": "false"
            }
        ]
    } ]} 

For two array data this code is not working ? please help

0 Kudos

Hi Sriprasad,

I tried the script you provided but I am getting the below error.

java.lang.NoSuchMethodException: No signature of method: groovy.json.internal.LazyMap. is applicable for argument types:

Could you please help with this.

Regards,

Akshay

beverely_parks2
Participant
0 Kudos

This is what worked for me:

import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.*
def Message processData(Message message)
{
def jsonbody = message.getBody(java.lang.String) as String;
def jsonSlurper = new JsonSlurper();
def jsonDataObject = jsonSlurper.parseText(jsonbody);


//Convert the JSON String to Int
//if (jsonDataObject.check_transaction_code!= null){
    jsonDataObject.check_transaction_code = convertToInt(jsonDataObject.check_transaction_code);
    
    //Integer conversion  
    message.setBody(new JsonBuilder(jsonDataObject).toString());
    return message;
//}
}


   static Object convertToInt(Object inputValue){
        if(inputValue.isNumber()){
          return inputValue.toInteger();
        }else{
          return inputValue;
        }
   }

former_member213386
Active Participant
0 Kudos

Hi Beverly, the processData function needs always to return a message, so your brackets around check_transaction_code should only be around your convert statement , also you could check if you incoming message would really return a single object or maybe an array of objects, else your approach looks fine. br, Sophie.

0 Kudos

Hi Beverley,

If you have message mapping, then try to place the condition and can control there. Use exist function and try to achieve this. If not working reply back, will try other suggestions.

Thanks,

Loganathan R