cancel
Showing results for 
Search instead for 
Did you mean: 

Array in JSON - REST Adapter

former_member210091
Participant
0 Kudos

Hey All,

I am using Receiver REST Adapter to send JSON. Most of my JSON is correct except the Array. I have already enabled the setting in communication channel but I am still not getting array.

Can someone please help:

Accepted Solutions (1)

Accepted Solutions (1)

former_member210091
Participant
0 Kudos

Finally heard from SAP.

Array for single elements is available with newer Support Packages. Please see note 2175218.

Thanks,

Answers (1)

Answers (1)

vadimklimov
Active Contributor
0 Kudos

Hello Saif,

In communication channel configuration, in XML/JSON conversion rules section, XML namespace and prefix of the element 'events' are missing - please specify them and test again.

Regards,

Vadim

former_member210091
Participant
0 Kudos

Hey Vadim,

Please advise if namespace and prefix are mandatory?

I don't have them in my payload:

Former Member
0 Kudos

Saif

What my guess is that it should be mandatory as while conversion the adapter never connects with ESR to verify the metadata hence does not have clue on what type of field is converted to what. It's totally on basis what information you have provided in XML/JSON Conversion. Neither adapter uses the XPATH to verify the field while conversion.So while conversion the adapter should know at what level of data the conversion is taken place and to what format. And namespace and prefix is the only way to know that apart from XPATH.

Eg. Suppose in above situation you have two events field defined but at different level in data. One field you have defined with only one occurrence and lies within element Payment  then just by providing name do you think you can convert the above xml to right JSON.

Now irrespective of case whether its mandatory or not, you can use XSLT Mapping and introduce namespace and prefix in the data and hence can use the XML/JSON Conversion.

- Abhishek

former_member210091
Participant
0 Kudos

Hey Abhishek/Vadim,

I have added namespace but still don't get array. Please advise what else I need to check:

Thanks

vadimklimov
Active Contributor
0 Kudos

Hello Saif,

In your XML message, the element 'events' is a root element, and as per XML specification, the well formed XML document can have one and only one root element.

To overcome this issue, I suggest you using one of nested elements of the XML document as array entries - looking into the XML document, the element 'data' is suitable for this role:


<events>

  <data>

       <eventContext>

            ...

       </eventContext>

       ...

  </data>

  <data>

       <eventContext>

            ...

       </eventContext>

       ...

  </data>

</events>

Or alternatively, you can slightly change name of the child element 'data' to 'event' so that it is more consistent with the root element name:


<events>

  <event>

       <eventContext>

            ...

       </eventContext>

       ...

  </event>

  <event>

       <eventContext>

            ...

       </eventContext>

       ...

  </event>

</events>

Following this approach, special handling can be enabled for 'data' (in the first case) or 'event' (in the second case) so that they are treated as arrays by REST adapter during XML/JSON conversion.

Regards,

Vadim

former_member210091
Participant
0 Kudos

Hey Vadim,

But in that case, I won't be able to add array for "events", which is how the external vendor wants.

If I add a root element called "root" and check "Strip Outer Element" in comm channel, will it work?

Do I still need namespace?

former_member210091
Participant
0 Kudos

So I finally got it working but will check with SAP for some clarification.

As per following SAP document(http://help.sap.com/saphelp_nw73ehp1/helpdata/en/a4/0979890af3403d84ff2f028ffec5d9/content.htm) ,

namespace is only required if there are more than 2 elements with same name (since I only had one, I did not give namespace).Secondly, array is only created if there are more than one occurrence of same element. If the element occurs only once, array won't be created (I don't like this so will check with SAP on it).

I did a duplicate subtree in message mapping (thereby creating multiple "events" tag in XML) and it created array in JSON. Our external vendor is fine with receiving same JSON twice but I don't think it's the best solution.

Will update the ticket with SAP's response.

Thanks

Former Member
0 Kudos

thanks Saif for sharing the details. Do let us know if you get any update from SAP

former_member194786
Active Contributor
0 Kudos

Hi Saif,

I had a similar requirement and ultimately decided to go for Java mapping to create JSON content, specially considering my output structure was very small (8-10 fields at the most). And in the communication channel I didn't perform any conversion.

Hope this is useful for anyone else looking for similar solution.

Regards,

Sanjeev.

bhavesh_kantilal
Active Contributor
0 Kudos

I like Sanjeev's answer.

On a PI 7.1 server we had to convert JSON to XML and ended up using a Java Mapping.

All we had to do was use the org.json.JSONObject class from json.org and with 2 lines of code, our JSON was converted to XML.


//inputStreamString is the JSON as a String.

// Use Java your normal Java Mapping Techniques to convert InputStream to String

JSONObject o = new JSONObject(inputStreamString);

  String xml = org.json.XML.toString(o);

I ran the your JSON examples through this and it works like a charm!

Regards,

Bhavesh

bhavesh_kantilal
Active Contributor
0 Kudos

Oops, I read this all wrong. The need is to convert XML to JSON and not the other way around. My bad.. bummer!

Regards,

Bhavesh