Monday 23 October 2017

JSON Code for conversion of String to Integer/ Remove double quotes from JSON:

All input data will consider as the String after converting from the XML to JSON, as the output sends to database which supports only without double quoted for the integer. This can be achieved by the below code

IMG:001

Add the groovy script after the XML to JSON Converter then it would work.

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

//Convert the JSON String to Int
jsonDataObject.Contact.id = convertToInt(jsonDataObject.Contact.id);
jsonDataObject.assignedTo.account.id = convertToInt(jsonDataObject.assignedTo.account.id);
jsonDataObject.assignedTo.staff.id = convertToInt(jsonDataObject.assignedTo.staff.id);
jsonDataObject.c.apf = convertToBoolean(jsonDataObject.c.apf);

//Integer conversion  
message.setBody(new JsonBuilder(jsonDataObject).toPrettyString())
return message
}
   static Object convertToInt(Object inputValue){
        if(inputValue.isNumber()){
          return inputValue.toInteger();
        }else{
          return inputValue;
        }
   }}

//Boolean Conversion
static Object convertToBoolean(Object inputValue){
        if(inputValue.equals("true")){
          return true;
        }else{
          return false;
        }

Input: "id":"3261390"
Output: "id":3261390

Issue fix:
If there is no value for the fields then it could not handle that, throws the error eg: ” property id : line 20 it cannot convert null value”

Instead of

jsonDataObject.assignedTo.account.id = convertToInt(jsonDataObject.assignedTo.account.id);

Add the below code:

if(jsonDataObject.assignedTo!= null)
{if(jsonDataObject.assignedTo.account.id!=null)
{  jsonDataObject.assignedTo.account.id = convertToInt(jsonDataObject.assignedTo.account.id);}}




2 comments:

  1. Thank you for bringing more information to this topic for me. I’m truly grateful and really impressed.

    word to xml conversion online

    ReplyDelete
  2. Hello I am getting below error,

    java.lang.NoSuchMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.convertToInt() is applicable for argument types: (java.lang.String) values: [1.0]

    My Json Structure is as below (simplified my structure to get the pin point solution),

    {"internalId":"xxxx","customerId":"xxxxx","documentType":"x","dateTimeIssued":"2021-06-21T12:21:44","currency":"USD","currencyRate":"1.0","extraDiscountAmount":"1"}

    Want to convert it for currencyRate and extraDiscountAmount.

    Wrote below code but not working for me,

    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(java.lang.String) as String;
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)

    list.currencyRate = convertToInt(list.currencyRate);

    def jsonOP = JsonOutput.toJson(list)
    message.setBody(jsonOP)

    return message;
    }

    ReplyDelete