Skip to Content

Update 31 Mar 2016: Enhancement to support forcing of JSON output in arrays irrespective of number of values in payload with new parameters forceArrayAll or arrayFieldList

Update 25 Mar 2015: JSONTransformBeanBean has been refactored to be part of FormatConversionBean. Parameter conversionType replaced with converterClass. Note that some of the screenshots are not updated to reflect the new parameter converterClass

Introduction

This is the second part of the two-part series on conversion of JSON content with a custom adapter module. The first part covered the conversion of JSON to XML at a sender channel.

This second part will focus on conversion of XML to JSON at a receiver channel.

Source Code

Refer to following blog on location of source code and/or EAR deployment file.

FormatConversionBean – One Bean to rule them all!

Usage of Module in Communication Channel

Module Processing Sequence

Number Module Name Type Module Key

<Depending on position of module in chain>

1) Asynchronous scenario,

Normally before the last module in channels

2) Synchronous scenario,

Before last module to convert request payload

After last module to convert response payload

Custom_AF_Modules/FormatConversionBean Local Enterprise Bean <Any Arbitrary Value>

Module Parameter Reference

Below are the parameters for configuration of the module for XML to JSON conversion (converterClass = ‘com.equalize.xpi.af.modules.json.XML2JSONConverter’). Certain parameters will automatically inherit the default values if it is not configured.

Parameter Name Allowed values Default value Remarks
conversionType XML2JSON Required field. Determines conversion type
converterClass com.equalize.xpi.af.modules.json.XML2JSONConverter Required field. Determines conversion class
indentFactor Integer values beginning from 1 0

Determines the number of indentation spaces for each level in the JSON output.

Note: When indentation is not used, the JSON content produced is compact and ideal for lightweight data transfer

skipRootNode Y,N N Skips root XML element in JSON output
forceArrayAll Y,N N Forces all nodes and fields to be in JSON Array
arrayFieldList

Comma separated values of field names to be forced as JSON Array.

Example: field1,field2,field3

messageLog

pre, post

Saves a log version of the message that is viewable in Message Monitor

  • pre = saves payload before conversion
  • post = saves payload after conversion
logLocation Name of log version when messageLog is populated. Location defaulted to value in messageLog if logLocation not populated

Example

Module parameters

Parameter Name Parameter Value
converterClass com.equalize.xpi.af.modules.json.XML2JSONConverter
skipRootNode Y
indentFactor 2

Result

Input

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

<ns:MT_XML2JSON xmlns:ns=”urn:equalize:com”>

<glossary>

  <title>example glossary</title>

  <GlossDiv>

   <GlossList>

    <GlossEntry>

     <SortAs>SGML</SortAs>

     <GlossDef>

      <GlossSeeAlso>GML</GlossSeeAlso>

      <GlossSeeAlso>XML</GlossSeeAlso>

      <para>A meta-markup language, used to create markup languages such as DocBook.</para>

     </GlossDef>

     <GlossTerm>Standard Generalized Markup Language</GlossTerm>

     <GlossSee>markup</GlossSee>

     <ID>SGML</ID>

     <Acronym>SGML</Acronym>

     <Abbrev>ISO 8879:1986</Abbrev>

    </GlossEntry>

   </GlossList>

   <title>S</title>

  </GlossDiv>

</glossary>

</ns:MT_XML2JSON>

Output /wp-content/uploads/2015/03/out1_666636.png

Screenshots of actual configuration and testing are also shown below.

Module configuration on an SFTP receiver channel.

/wp-content/uploads/2015/03/param2_666686.png

The XML input payload.

/wp-content/uploads/2015/03/in1_666690.png

The ‘jsonContent’ log version shows the JSON content after conversion. The JSON content produced is compact since parameter indentFactor was not used.

/wp-content/uploads/2015/03/json_666689.png

The audit log shows the trace of steps being executed by the module.

/wp-content/uploads/2015/03/log_666692.png

Conclusion

With the completion of this second part for JSONTransformBean, we now have a generic and reusable custom adapter module that is able to handle conversion of JSON to XML and vice versa! Bring on the RESTful web services!

To report this post you need to login first.

25 Comments

You must be Logged on to comment or reply to a post.

  1. Juan Vasquez

    Hello

    It is a nice module, but i have a problem with custom headers

    Can you please help me

    2015-04-28 13_43_34-Message Editor - SAP NetWeaver Administrator.png

    i need some custom headers for the call

    Content-Type: application/json

    Cache-Control: no-cache

    Application-Secret:5554b65aaaacb2ed74cc7d9c5921aaaa2314aacd

    what is the right receiver adapter soap?, http (axis)?

    But I don’t know here to add

    Thanis a lot

    (0) 
  2. Vipin Kanchan

    Hi Eng Swee,

    Great blog… Just wanted to check whether the converter class mentioned on this page ‘XML2JSONConverter‘ can help in forcing some of the json objects to be array values even though they are supplied with single values in some payloads?

    Is it possible to force the square brackets via the module?

    (0) 
    1. Eng Swee Yeoh Post author

      Hi Vipin

      That feature is not available in the current XML2JSONConverter. There is no straightforward method to determine which XML single values should be JSON arrays or not. It is something that requires further thought in the design in order to incorporate it properly into such a generic converter – unfortunately, I don’t have the liberty of spending time to enhance it at the moment.

      Since the source code for my converters are available as open source, you can consider downloading them and enhancing the module specific to your requirement. You can refer to the following blog on how to do download and recompile it.

      Recompile com.equalize.xpi.af.modules as EJB 2.1 modules in NWDS 7.1x

      Rgds

      Eng Swee

      (0) 
        1. Eng Swee Yeoh Post author

          Hi Vipin

          You are welcome. It should be quite straightforward to download and setup the module project in your NWDS environment. All the best in your efforts 😉

          Rgds

          Eng Swee

          (0) 
  3. vk k

    HI Eng Swee,

    Thanks a lot for the excellent Blog.

    I will try to use the XML2JSON Converter from this.

    Can you please help me in the deployment of the same.

    Where Exactly I can see the .ear file ? Currently, I am not able to find it.

    Once I have it, i will try to deploy the same directly in my NWDS..

    Regards,

    Vk

    (0) 
      1. Peter Chezowitch

        Hi Hareesh, Hi Eng Swee,

        How can we Import the source Code on a 7.31 system? I saw a manuel for doing it with EJB 2.1 on PI 7.1, but if we have a 7.31 System we can use the EJB3 as Eng Swee used for the original development.

        I am just not sure how the procedure is. Once I downloaded and extracted the source Code, do we need to create an EJB3 Project in NWDS manually and then add the source Code or is somewhere a complete NWDS Project available for Import?

        (0) 
        1. vk k

          HI Peter,

          There is .ear available and all you need to do is just to deploy the same on your PI server using NWDS or some other means.

          It is available in the very beginning of the blog.

          Regards,

          Vk

          (0) 
        2. Eng Swee Yeoh Post author

          Hi Peter

          The ready to deploy EAR file is compiled for 7.31/7.4 so you can use it directly if you do not need further customisation. As mentioned by VK, refer to the Source Code section, it points to the following blog which provides you the details for deployment. If you need to change the logic, there is also further instruction on how you can build your own project.

          FormatConversionBean – One Bean to rule them all!

          Rgds

          Eng Swee

          (0) 
  4. Fabian Engel

    Hello Eng Swee,

    thank you for providing this valuable Custom module!

    I would like to integrate it in my synchronous Scenario:

    3rd party program calls a webservice on PI via SOAP Sender channel.

    This request calls a RFC in SAP ECC which gives a response.

    This response should be converted from XML to JSON.

    I tried to put the module in SENDER SOAP channel, but it doesn’t work.

    Only the Incoming Request XML was mapped to JSON and This fails of course while RFC call…

    I think these modules are only processed, while message gets IN.

    How can I set up the conversion to JSON for the response message before the webservice invoking program gets the response ?

    Do you have an Idea?

    Thank you and best regards,

    Fabian

    (0) 
    1. Eng Swee Yeoh Post author

      Hi Fabian

      I’ve not used this on the sender channel for synchronous response before.

      First things first, which PI version are you on because I know earlier versions like 7.11 do not support adding modules on the SOAP sender channel.

      Secondly, when using modules to deal with synchronous response, the modules need to be placed after any existing standard modules in the channel. I normally place this after the standard module at the receiver channel. You can try this or even at the sender channel.

      Rgds

      Eng Swee

      (0) 
  5. Midhun Madhav

    Hi Eng Swee

    I am doing a REST — PO — ECC synchronous scenario. REST is using GET method to get some data from the ECC. In each set of data, PI is sending a url also in the payload.

    When the xml is converted to JSON, the url is escaped.

    http://host:port/RESTAdapter/xyz

    becomes

    http:\/\/host:port\/RESTAdapter\/xyz


    Sender synchronous REST adapter does this. Is there any way I can pass forward slash without escaping


    Regards


    Midhun

    (0) 
  6. sri barghava

    Hi Eng Swee,

    great work and thank you so much for the wonderful converter modules.

    We are trying to use the XML to JSON conversion module in our REST adapter. The module is interpreting everything as a string and while its converting it is not considering the data types from the xml input. so the JSON is actually failing at the receiver end, because we are sending string in the integer/Boolean/float variables. And there is no type conversion at their end.

    Any suggestions from your end?

    Thanks,

    Vijaya Palla

    (0) 
    1. Eng Swee Yeoh Post author

      Hi Vijaya Palla or Sri Barghava

      None of the XML to JSON converters (SAP’s, Advantco’s or mine) will check the field type from the XML definition. At the moment, yes my converter will output all fields as string in the JSON output.

      If you already have the REST adapter, I’d suggest you use SAP’s standard feature to perform enhance XML/JSON conversion that allows you to specify certain fields to be of a specific type. Refer to the following blog for more details.

      REST Adapter in PI/PO: Enhanced XML/JSON Conversion

      Regards

      Eng Swee

      (0) 
      1. sri barghava

        Hi Eng Swee,

        Thanks for the reply.

        We actually went with REST adapter since, our rest service is synchronous and they are returning us JSON. so to convert that to xml, we used the SAP’s standard feature of mapping conversion rules.

        I am not sure if REST itself can convert the input xml to JSON before hitting the rest service. we tried that out but it didn’t help us. so we had to go with the custom module given by you. But facing the schema validation issues.

        So was wondering, if there is any thing that can help us generating the JSON payload using XSD schema. any help?

        Thanks,

        Vijaya Palla

        (0) 
        1. Eng Swee Yeoh Post author

          Hi Vijaya Palla

          Firstly, as mentioned above, I’ve provided the link that shows how further conversion enhancement can be configured in the standard SAP adapter. SAP’s REST adapter provides XML to JSON functionality and I would strongly recommend that you try that out fully and raise a discussion thread in the forum in you have issues with that. The intention of my module is to cover the gaps where SAP’s solution do not provide such functionality, however in your particular case there is such functionality direct from SAP.

          Secondly, there is no publicly released API from SAP that provides functionality to access the XSD schema in ESR from an adapter module. As such, at the moment it’s not possible to include the feature to have a JSON payload generated based on XSD schema field types.

          Regards

          Eng Swee

          (0) 
            1. Eng Swee Yeoh Post author

              Hi Monica

              Adapter modules don’t have access to the HTTP header. You might want to update your SP level so that the version of the REST adapter has HTTP header functionality.

              (0) 
  7. S RAVINDER

    Hi Eng Swee,

    Very nice Blog.

     

    I am having the scenario to download the .tar.zip file using the URL( ECC — PI — REST)  In the request we are sending the URL for file download .This ZIP file contains the .JSON  file. In the request we are sending the url to download the zip file but,we are getting the erro.

    We thought that develop a Module For REST receiver Channel. Hear we are facing challenges for the deveopment.

    Below are my Queries.

    1. what is the input for module development as of now REST receiver channel downloading the ZIP file but while unzipping we are facing the problem.
    2. How we need to map the map the download zip file to the java code which is developed in module.
    3. My java team is asking  how are what he need consider the input for his ejb development and how this resposne willl mapp is there  any standard class or paramerte need to use  ? what is it.
    4. how we need to do response caputre in the module and what is standard calss?
    5. we are very new to the module development .

    Please help us to achive the requirment .

     

    Regards

    Ravi.S

    7893366122

     

     

    (0) 
  8. Pandey Anuj

    Hi Eng,

     

    I encountered issue with Chinese characters like after transformation from xml to Json it shows ‘?’ any idea about it ? could it be due to encoding in program?

    Appreciate your inputs.

    Thanks

    Anuj

    (0) 
  9. Gaurav Batra

     

    Hi All,

    I have a scenario (REST–>PI–>REST) wherein PI will receive the data from sender in JSON format. Data format accepted by receiver is also a JSON format.

    The input payload to PI looks like:

    [{
    “ClassCode”: “”,
    “FSAC”: “”,
    “TrainId”: “”,
    “UserId”: “”,
    “Equipment”: [{
    “EquipmentInitials”: “”,
    “EquipmentNumber”: “”,
    “EquipmentDisplayType”: ” “,
    “SourceId”: “”,
    “SourceType”: “”
    }, {
    “EquipmentInitials”: “”,
    “EquipmentNumber”: “”,
    “EquipmentDisplayType”: ” “,
    “SourceId”: “”,
    “SourceType”: “”
    }]

    }]

    When this input is passed to receiver Rest Adapter, I see that PI append root to it which is not acceptable by REST Service.

    JSON at receiver adapter looks like below:

    {
    “root”: [{
    “ClassCode”: “”,
    “FSAC”: “”,
    “TrainId”: “”,
    “UserId”: “”,
    “Equipment”: [{
    “EquipmentInitials”: “”,
    “EquipmentNumber”: “”,
    “EquipmentDisplayType”: ” “,
    “SourceId”: “”,
    “SourceType”: “”
    }, {
    “EquipmentInitials”: “”,
    “EquipmentNumber”: “”,
    “EquipmentDisplayType”: ” “,
    “SourceId”: “”,
    “SourceType”: “”
    }]

    }]
    }

    How to ignore this root element and add outer Array[], similar to the input payload.

    Thanks in advance!!

    Best Regards,

    Sachin Jangir

    (0) 
  10. Anurag Gupta

    Hello Eng Swee Yeoh

    I am providing it an xml where

    <carrierId>8</carrierId>
    <locationId>200</locationId>
    <maxDays>8</maxDays>
    <minDays>8</minDays>
    <supplierId>200</supplierId>

    but it is converting them to string as below 

    “carrierId”: “8”,
    “locationId”: “200”,
    “maxDays”: “8”,
    “minDays”: “8”,
    “supplierId”: “200”

    and this is what i do not want. I want them without quotes, that is integers.

    (0) 
    1. Eng Swee Yeoh Post author

      Hi Anurag

       

      This feature is not available currently. Feel free to download the source code and modify it accordingly to achieve your requirement.

       

      Regards

      Eng Swee

      (0) 

Leave a Reply