Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
engswee
Active Contributor

Update 23 Jun 2020: Add new parameter arrayGPathList to force JSON output in arrays using GPath notation

Update 5 Nov 2019: Add new parameter trim to trim XML text nodes

Update 1 May 2019: Add new parameter fieldConversions to generate output other than String

Update 2 Nov 2018: Default parsing in CPI will be using SAX-based XmlSlurper

Update 4 Sep 2018: Now available in CPI as well.

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
converterClass

PI - com.equalize.xpi.af.modules.json.XML2JSONConverter

CPI - com.equalize.converter.core.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
arrayGPathList

Available only in CPI

Comma separated values of field names to be forced as JSON Array. This uses GPath notation based on the target JSON structure

Example: field1,parent.field2,parent.child.field3
useDOM Y, N N

Available only in CPI

Parser used in parsing input XML

  • N = SAX-based XmlSlurper

  • Y = Document Object Model (DOM) based parser


trim Y, N N

Available only in CPI

Trim whitespace in XML text nodes during parsing
fieldConversions

Available only in CPI

Comma separated values of field names to be generated as a different type. Each entry is a key-value pair separated by colon (:)

Supported types are: number, null, boolean

Example: field1:number,field2:null,field3:boolean
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


Available only in PI
logLocation

Name of log version when messageLog is populated. Location defaulted to value in messageLog if logLocation not populated.

Available only in PI


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


Screenshots of actual configuration and testing are also shown below.

Module configuration on an SFTP receiver channel.



The XML input payload.



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



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



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!

49 Comments
Labels in this area