cancel
Showing results for 
Search instead for 
Did you mean: 

JSON to XML conversion in receiver REST communication channel

Former Member
0 Kudos

Hi Experts,

I am currently working on a Proxy to REST (synchronous) scenario, and have used SAP's REST adapter in the receiver communication channel.

I am using the GET operation here on the 'Waste-streams' resource of the target system's REST API, and am trying to retrieve all the Waste Streams for which the ChapterCode field is '01'.

This REST service of the target's system seems to be accepting the source message in XML format, but sends back a response message in the JSON format only. Hence I need to convert this JSON response into an XML structure that would match the response data type that I have created in the ESR. However, in this JSON to XML conversion, I am getting the following error:

com.sap.engine.interfaces.messaging.api.exception.MessagingException: com.sap.aii.adapter.rest.ejb.json.ConverterException: Error validating JSON input at com.sap.aii.af.app.listener.AFWListenerBean.onMessage

A JSONObject text must begin with '{' at character 1 of [{"ChapterCode":"01","Description":"wastes resulting from exploration,mining,quarrying,and physical and chemical treatment of minerals","StreamCode":"","SubChapterCode":""},{.... (followed by the entire JSON payload that the REST service sends back.

Below are the details that I have used in the Data Format tab of my receiver communication channel:

Here, 'Response' is the root node of the Message type created in the ESR to accept the response message that the service sends back.

Please can someone help me out in resolving this issue.

Regards,

Jyotika.

Accepted Solutions (1)

Accepted Solutions (1)

iaki_vila
Active Contributor
0 Kudos

Hi all,

I think for the benefit of the community someone should raise this problem to SAP in order to know if the JSON array is really not supported by Rest adapter or if they are thinking for example to include a new box to set the XML root tag manually for these cases.

Regards.

Former Member
0 Kudos

Hi Inaki,

I have written to Alex and Ivo to check on this.

Will let you know of their response on this query.

Thanks,

Jyotika.


engswee
Active Contributor
0 Kudos

Hi Inaki

Just to clarify, I think JSON Array is supported, but maybe just not at the top level for SAP's REST adapter case. Even for Advantco's REST adapter, it requires an additional setting to set the name of the top level array. Without providing a name for the JSON Array, the equivalent XML would be like below - a segment without name, which is invalid.


  <>

    <SubChapterCode>0101</SubChapterCode>

    <Description>Description 3</Description>

    <StreamCode>010101</StreamCode>

    <ChapterCode>01</ChapterCode>

  </>

Jyotika,

Let us know what response you get from Alex/Ivo.

Also alternatively, if you are willing to consider, I can update JSON2XMLConverter converter (refer link below) to support JSON array at the top level. You can then use this in the module chain of the receiver channel to convert the response. You can get a converted XML like the one below. Let me know if you wanna give that a try.


<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<ns:MT_JSON2XML xmlns:ns="urn:equalize:com">

  <record>

    <SubChapterCode/>

    <Description>Description 1</Description>

    <StreamCode/>

    <ChapterCode>01</ChapterCode>

  </record>

  <record>

    <SubChapterCode>0101</SubChapterCode>

    <Description>Description 2</Description>

    <StreamCode/>

    <ChapterCode>01</ChapterCode>

  </record>

  <record>

    <SubChapterCode>0101</SubChapterCode>

    <Description>Description 3</Description>

    <StreamCode>010101</StreamCode>

    <ChapterCode>01</ChapterCode>

  </record>

</ns:MT_JSON2XML>

Rgds

Eng Swee

Former Member
0 Kudos

Sure Eng Swee,

Will be happy to try out the above solution proposed by you.

Thanks,

Jyotika.

engswee
Active Contributor
0 Kudos

Hi Jyotika

I have updated and released a new version of the converter class.

After you have deployed the custom adapter module, add the following module AFTER the last module in the REST adapter in order to process the response message. This will generate the above XML payload based on your JSON input. You can adjust some of the names according to your interface requirements. Make sure that you also disable the JSON to XML conversion in the SAP REST receiver channel.

Module NameTypeModule Key
Custom_AF_Modules/FormatConversionBeanLocal Enterprise Beanjson2xml

Module KeyParameter NameParameter Value
json2xmlconverterClasscom.equalize.xpi.af.modules.json.JSON2XMLConverter
json2xmldocumentNameMT_JSON2XML
json2xmldocumentNamespaceurn:equalize:com
json2xmlindentFactor2
json2xmlallowArrayAtTopY
json2xmltopArrayNamerecord

For further reference on the usage of the converter, refer to the following document.

For more information (where to download the latest source code or EAR deployment file) on the parent module bean, refer to the following blog

Rgds

Eng Swee

Former Member
0 Kudos

Thank you so much Eng Swee. We will implement the above module and will get back to you once done.

Regards,

Jyotika.

Former Member
0 Kudos

Hi Eng Swee,

Just had a few queries about the above solution. I tried to create a new EJB project and the EAR in NWDS myself, thinking that i would copy the source code available on your repository. But somehow, I am unable to obtain all the necessary jars required for it to run successfully.

So then I was wondering if I could simply get the EAR (shared by you at: https://github.com/engswee/com.equalize.xpi.af.modules/releases/tag/v20150402 ) deployed on my PI server by the BASIS team. Please can you let me know if I can get this done. Also, how do I attach the source code (zip) and source code (tar.gz) files to the EAR?

Additionally, I would like to share that we are currently on PI 7.4 (dual stack) system - SP10, and our NWDS is not connected with the PI server. So, the only way I can have the EAR deployed is by asking the BASIS team to do so. Hope this is fine.

Also, can I request you to please share your email address with me, so that it's more convenient to communicate.

Regards,

Jyotika.

engswee
Active Contributor
0 Kudos

Hi Jyotika

As your system is on a higher SP than mine (7.4 SP08), I think it should not be an issue to just deploy the EAR file directly.

You don't need the zip and tar.gz files for deployment. Just the EAR will be sufficient, and yes, it's okay for Basis to deploy it. managed to deploy it on his system but not sure if he recompiled everything or just used the EAR file directly.

As for communications, I'd prefer to keep it within SCN. If you want to, you can follow me and we can communicate via SCN Direct Message.

Rgds

Eng Swee

Answers (1)

Answers (1)

iaki_vila
Active Contributor
0 Kudos

Hi Jyotika,

You JSON structure is wrong, could share it?, in this way we can check where the problem can be.

Regards.

Former Member
0 Kudos

Hi Inaki,

The message being sent back in response is as follows:

[{"ChapterCode":"01","Description":"Description 1","StreamCode":"","SubChapterCode":""},{"ChapterCode":"01","Description":"Description 2","StreamCode":"","SubChapterCode":"0101"},{"ChapterCode":"01","Description":"Description 3","StreamCode":"010101","SubChapterCode":"0101"}]

It has content type - application/xml and

         charset - UTF-8

Would it be the square brackets '[' and ']' at the start and end of the message that would be causing the problem?

Would it help to enable the checkboxes:

    1. Escape invalid name start character

or 2. Mangle invalid name characters.

Thanks,

Jyotika.

iaki_vila
Active Contributor
0 Kudos

Hi Jyotika,

I think you should construct a right JSON. If you are using arrays you need to define something like:

{

"data":[{"ChapterCode":"01","Description":"Description 1","StreamCode":"","SubChapterCode":""},{"ChapterCode":"01","Description":"Description 2","StreamCode":"","SubChapterCode":"0101"},{"ChapterCode":"01","Description":"Description 3","StreamCode":"010101","SubChapterCode":"0101"}]

}

Im not an expert about JSON but you there are some interesting links like this JSON Syntax

Regards.

engswee
Active Contributor
0 Kudos

Hi Jyotika

The square [] brackets represents a JSON Array and it is a valid character for JSON syntax (refer below)

JSON

However, JSON to XML conversion is a little bit trickier because the conversion has to follow XML rules. A JSON array is somewhat similar to a recurring XML element (with maxOccurs > 1).

There are two things in the JSON content might be causing the issue.

1. I think the SAP REST adapter does not support JSON Array at the first level of the hierarchy. It expects a JSON object (which starts with curly brackets {}) at the top level. With a JSON Array at the top level, there is no "name" associated with each line in the array - something like an unnamed recurring XML element, which is impossible. Per Inaki's suggestion, you need to encapsulate the JSON array in a JSON object . Example:

{ "arrayLine" : [<JSON array content here>] }

2. Secondly, even with the JSON array named, it will try to convert all the occurrence of "arrayLine" into the root element of the XML document. This is not allowed XML only permits a single root element. Refer to the description in the following blog. Ensure that you have the Element Name & Namespace populated.


The two JSON elements Agency and FlightDetails are both on the very first level of hierarchy which is allowed for JSON however not for XML. XML messages can have one root element only. If the JSON payload is converted into XML, this would not comply with the XML specification.

For the first issue, you can try checking with the service provider is there is a possibility to have the response beginning with a JSON Object instead of a JSON Array.

Rgds

Eng Swee

Former Member
0 Kudos


Thanks Inaki and Eng Swee for your response.

I will check with the third party service provider if they can re structure the response message such that it starts with the JSON Object instead of JSON Array.

Will get back to you in case I need more help.

Thanks,

Jyotika.