Skip to Content

SAP Cloud Integration Groovy script to convert JSON string to integer

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?

Add a comment
10|10000 characters needed characters exceeded

Related questions

5 Answers

  • Best Answer
    Posted on Dec 14, 2018 at 03:09 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 17, 2018 at 09:21 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 15, 2018 at 10:14 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 17, 2018 at 08:23 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 10 at 01:59 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • 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;
              }
         }
      

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.