cancel
Showing results for 
Search instead for 
Did you mean: 

REST Adapter: Problem converting XML containing arrays to JSON

former_member191660
Participant
0 Kudos

Hi all,

we are using REST as a Sender Adapter. Works (almost ) like a charm

The problem we are facing is that we have an Array-like Element in the Repsonse XML DataType. Depending on the amount of actual elements in the array, the element gets converted to a JSON Array or JSON OBject.

E.g. If the element contains exactly 1 item, it is converted to a JSON object. If it contains more than one item, it is comverted to an array. if it contains no elements, it is not contained in JSON at all.

Case 1: More than one element in XML

consider following response XML:


<resultlist>

  <item><value>item 1</value></item>

  <item><value>item 2</value></item>

  <item><value>item 3</value></item>

</resultlist>

it gets correctly converted to


{

    "result": {

        "item": [

            {  "vlaue": "item 1"  },

            {  "vlaue": "item 2"  },

            {  "vlaue": "item 3"  }

        ]

    }

}

Case 2: Single element in XML

consider following response XML:


<resultlist>

  <item><value>item 1</value></item>

</resultlist>

it gets NOT correctly converted to


{

    "result": {

        "item":

            {  "vlaue": "item 1"  }

    }

}

expected result:


{

    "result": {

        "item": [

            {  "vlaue": "item 1"  }

        ]

    }

}

Case 3: No elements in XML

consider following response XML:


<resultlist/>


it gets NOT correctly converted to


{

    "result": ""

}

expected result:


{

    "result": {

        "item": []

    }

}

Bottomline

The point is that the consumer of the result cannot rely on the structure of the content. Sometimes the element is just en element, sometimes it is an array, sometimes it isn't even there...

The issue has been addressed at the following locations, but I havent found a solution and/or a note yet...

  • [Comment by dated 2015-03-17]

Thanks and best regards,

Sergei

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member191660
Participant
0 Kudos

I was going to add the relevant XSD definition part to the original post, but I cant find the edit button

So there we go, as a reply:

         <xsd:element name="resultlist">

            <xsd:complexType>

               <xsd:sequence>

                  <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">

                     <xsd:complexType>

                        <xsd:sequence>

                           <xsd:element name="value" type="xsd:string"/>

                        </xsd:sequence>

                     </xsd:complexType>

                  </xsd:element>

               </xsd:sequence>

            </xsd:complexType>

         </xsd:element>

Former Member
0 Kudos

Hi,

Were you able to get the JSON array for a single entry ?\

Thanks

Ravijeet

vadimklimov
Active Contributor
0 Kudos

Hi, Sergei and Ravijeet,

Please have a look into recently released SAP Note 2175218 which describes implemented enhancement for XML to JSON conversion logic in REST adapter. Usage of configuration table and specifying XML element "item" to be always recognized as an array shall fulfil your requirement.

Regards,

Vadim

former_member191660
Participant
0 Kudos

Hi Vadim,

thank you very much for the hint. can't try it right away, though.

are you involved in the implementation in any way? I am wandering, why the special configuration file. I think (as I wrote above) the array information can easily deduced from the XSD

Thanks and best regards,

Sergei

vadimklimov
Active Contributor
0 Kudos

Hi Sergei,

No, I'm not involved in implementation of REST adapter - but I'm involved in its configuration and usage in currently ongoing projects on customer side. I looked into few pieces of its source code to get some clue about its implementation, because I wanted to get better understanding regarding its internals when we started using it in projects. So, unfortunately I cannot comment on what the reason was behind implementing configuration for this kind of handling as a configuration table in communication channel. I may only assume it was more convenient or efficient to implement it in such a way. Underlying JSON processor that is used by REST adapter, is Jettison - and Jettison doesn't recognize XSD schema of an XML document which it converts in JSON. And Jettison has its own type conversion logic which sometimes leads to unexpected result (like one described by you: when originally defined array is converted to a JSON object, if an array has only one element - simply because Jettison doesn't know it is actually an array and treats it as a JSON nested object unless it finds multiple occurrences of it or is specifically instructed to treat it as an array). As far as I can see, there was extra logic put in place when calling Jettison: for specific XML elements (those specified in newly introduced configuration table), special properties where set and then passed to JSON converter configuration. In this way, for specific XML elements, XML events which are raised and then processed by Jettison when producing JSON output, are generated in a custom way, resulting in an enhanced and more accurate output. Again, it has nothing to do with original XSD of a processed message type, this all relies on content of configuration table that has been introduced with an enhancement for REST adapter mentioned above.

Regards,

Vadim

Former Member
0 Kudos

Hi Vadim,

We are currently on SAP PO 7.4 Service pack 11. We have tried to implement patch mentioned in below note 2175218 - New Feature: Enhanced XML/JSON conversion capabilities.

After implementing the patch I don't see the mapping table in the communication channel of Sender REST adapter.

Have they released any step by step configuration document on how to set values in this mapping table?

Thx in advance

Ravijeet

vadimklimov
Active Contributor
0 Kudos

Hi Ravijeet,

After you apply a patch from the Note 2175218, please also ensure that adapter metadata for REST adapter is updated - in other words, import recent XI Content for SAP Basis component in ESR. After doing that, you shall be able to see the newly introduced table for custom XML/JSON conversion parameterization:


Regards,

Vadim

Former Member
0 Kudos

Hi Vadim,

Thank you for your quick response. I updated my adapter meta data and I am able to see this mapping/ conversion table.

I tried to add in the name field the JSON/ XML field which I want to keep as string even when it carries integer value, what value should I pass in Type and Array Type ?

Also I have one node which I need to define as array even when it carries single values.

Thx in advance

Ravijeet

vadimklimov
Active Contributor
0 Kudos

Hello Ravijeet,

XML namespace - XML namespace of an element in XML document of PI message.

Prefix - XML prefix of an element in XML document of PI message.

Name - Name of an element in XML document of PI message.

Type - if you need to force an element to be parsed as String even if it has integer value, assign one of values string, xs:string or xsd:string.

Array type - if you need to force element to be parsed as an array, assign one of values 1, true or yes.

Regards,

Vadim

Former Member
0 Kudos

Hi Vadim,

Thank you for the information. I was able to upgrade and implement the array feature for single line and also the Integer String values.

Thanks

Ravijeet

fabian_mohr
Discoverer
0 Kudos

Hello Vadim,

we are struggling with the same Problem. Unfortunately we are not able to fix it, even with the new Adapter functionalety.

Do we Need to add a dedicated Namespace? Our JSON-Response doesn't have any Namespace:

Here is the current configuration.

Any clue why it is not working?

regards

Fabian Mohr

SergioG_TX
Active Contributor
0 Kudos

Sergei,

it seems like scenarios 1, 3 are correct. Scenario 2 doesnt make sense. is this the response from the same code? have you checked the path from the root on scenario 2? maybe it is reading the root therefore not seeing the array?

former_member191660
Participant
0 Kudos

Hi Sergio,

without context, the scenarios 2 and 3 would be both correct. (well, the scenario 3 we could argue whether it should really be the empty string or at least an empty object: {})

But the PI has the type defnition and KNOWS that <resultlist> is a list containing 0..* <item> elements.

Thanks,

Sergei