cancel
Showing results for 
Search instead for 
Did you mean: 

JSON empty value issue - OData Receiver

former_member183906
Active Contributor
0 Kudos

Hi,

CPI is sending JSON message as given below after message mapping and XML to JSON converter.

{
"EmpNr": "1234",
"EmpDetails":""
}

But, The SAP OData service works if JSON message is as given below. OData service works if EmpDetails have empty value in brackets [].

{
"EmpNr": "1234",
"EmpDetails": []
}

How can we convert "EmpDetails":"" to "EmpDetails": [] when sending JSON message to SAP OData service. Can anyone help in this.

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi vijayant.jha2,

please try below code:

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

def Message processData(Message message) {
    
    def body = message.getBody(String);
    def slurper = new JsonSlurper().parseText(body)
    def empNr = slurper.EmpNr
    def str = []
    def builder = new groovy.json.JsonBuilder()

    builder.root{
        EmpNr "$empNr"
        EmpDetails "$str"
    }
    
    message.setBody(builder.toString());
    
    return message;
    }


Hope this helps !

thanks and regards,

Praveen T

former_member183906
Active Contributor
0 Kudos

tirumareddy.praveen - Thanks for the script. It converts to array successfully. But when I post JSON to OData receiver,I am getting below error -

Error Details
com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occured: Request Payload Parsing Failed for one of the reason: (1) Either the OData metadata content available in the server is outdated OR (2) The request payload seems to be incorrect. Error Details : only whitespace content allowed before start tag and not { (position: START_DOCUMENT seen {... @1:1) , org.xmlpull.v1.XmlPullParserException: only whitespace content allowed before start tag and not { (position: START_DOCUMENT seen {... @1:1) .
The MPL ID for the failed message is :
For more details please check tail log.

Any idea on this.

Answers (2)

Answers (2)

Former Member
0 Kudos

Did you try the option "Streaming" in XML2JSON Converter?

But you have to specify your XML Element with the complete XPath, otherwise it won't work.

f.e.: /ns1:CATALOG/CD/COUNTRY (if you need to use a namespace, you must be define it in the Runtime Configuration.)

former_member183906
Active Contributor
0 Kudos

Thanks for reply.

Former Member
0 Kudos

Did it work for you?

former_member183906
Active Contributor
0 Kudos

It did not work for me. I declared namespace and gave path as mentioned too as per screenshots. I don't know why It did not work for me. Thanks for your reply.

former_member183906
Active Contributor
0 Kudos

dennissentler - The streaming option worked for me. I just gave XML element path without namespace, I din't declare any namespaces too. If we deselect namespace mapping, then there is no need to give namespace in XML element and it works. Thanks.

Former Member

Hello vijayant.jha2 , glad to hear that it worked for you.

Being a programmer, which I also am, it is always easier to work with scripts and code.

BUT in my opinion, the CPI, the users and also the customers benefit the most when the given on-board tools are used (ex: Content Modifier, Converter etc.). This way the maintenance is much easier and also the customer is more happy about a graphically readable result than about script snippets. Even if it is sometimes a bit more challenging to use the on-board tools, if you can avoid the use of a script, it is worth it.

I wish you all the best

Dennis

0 Kudos

Hi vijayant.jha2,

Seems the ODATA service is expecting an array instead of single element.

You can convert it to array using a groovy script.

hope this helps !

thanks and regards,

Praveen T

former_member183906
Active Contributor
0 Kudos

tirumareddy.praveen - Ok, Thanks for your reply.

Can you please help in groovy script to convert it to array.