DataPower

  • 1.  After Json conversation, Only XML values are passed in json request in datapower

    Posted Fri October 09, 2020 02:04 PM
    Edited by Raja Kumar Tue October 13, 2020 02:59 AM
    Hi Team,

    We've a requirement in DataPower where I send to XML payload in embedded json request by escaping double quotes in the xml payload. I've tired below steps 

    1. Created a MPGW service and sending xml payload in the below jsonx request. 

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" extension-element-prefixes="dp" exclude-result-prefixes="dp" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:dp="http://www.datapower.com/extensions">
        <xsl:template match="/">
    	     <json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd">
              <json:array name="records">
               <json:object>
                <json:string name="key">123456</json:string>
                <json:string name="value">
                 <json:object>
    			<xsl:copy-of select="."/> 
    	    </json:object>
                </json:string>
               </json:object>
              </json:array>
             </json:object>
         </xsl:template>
    </xsl:stylesheet>
    2. In the next step, I'm doing jsonx2json conversation which we have pre-built jsonx2json xsl file in datapower. Below is the output.




    In the above image, If you see, After jsonx2json conversation, Only XML values are passed and tags were missing. we need to send the complete xml as it is.. 

    Appreciate your help if anyone is having solution for this above issue. 

    Thanks in advance.

    ------------------------------
    Raja Kumar
    ------------------------------


  • 2.  RE: After Json conversation, Only XML values are passed in json request in datapower

    Posted Mon October 12, 2020 03:16 AM
    Edited by Raja Kumar Tue October 13, 2020 02:39 AM
    ​Hello
    try

    <json:object>

      <dp:serialize select="." omit-xml-decl="yes"/>

    </json:object>

    instead of

    <json:object>
       <xsl:copy-of select="."/>
    </json:object>

    ------------------------------
    D@viiid
    ------------------------------



  • 3.  RE: After Json conversation, Only XML values are passed in json request in datapower

    Posted Tue October 13, 2020 02:33 AM
    Thank you D@viiid. Yeah I've tired above option. It worked.

    ------------------------------
    Raja Kumar
    ------------------------------



  • 4.  RE: After Json conversation, Only XML values are passed in json request in datapower

    Posted Tue October 13, 2020 01:55 AM
    You can also try to use one GatewayScript transform:

    session.input.readAsXML(function(error, xml) { if (error) { console.error('readAsXML error: '+error); } else { //Set options so that XML declaration will be removed var option = { omitXmlDeclaration: true } ; //Transform into a string var xmlText = XML.stringify(option,xml); //"Escape double quotes" var escapedXmlText = xmlText.replace(/"/g,"'"); //Create the json output, not the nicest one, but just for the demo var json = '{"key":"12345","value":"' + escapedXmlText + '"}'; //Write json into output session.output.write(JSON.parse(json)) ; } })​
    Just a different approach that D@viid suggested. If you'll end up with the GatewayScript you might want to "escape" the line feeds also...

    --Hermanni

    ------------------------------
    Hermanni Pernaa
    ------------------------------



  • 5.  RE: After Json conversation, Only XML values are passed in json request in datapower

    Posted Tue October 13, 2020 02:35 AM
    Thank you Hermanni. I'll try this option also..

    ------------------------------
    Raja Kumar
    ------------------------------