on 12-13-2018 8:31 PM
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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
}
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
81 | |
25 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.