jsonxsltmappingibm-datapower

DataPower: Map Json response to xml using XSLTs


My SOR application is returning result set, I'm performing below binary-encode and decode to retrieve json

<xsl:variable name="response_json">
  <xsl:copy-of select="dp:decode(dp:binary-encode($resp/result/binary/node()), 'base-64')"/>
</xsl:varibale>

response_json - {"code":"00", "description":"Success"}.

Now how do we parse through above response_json and assign "code"/"description" values to xsl:variables and context variables using XSLTs


Solution

  • You have two options, IMO one good and one bad...

    Let's start with the bad one which you are already on as you are using XSLT for JSON data.

    Even worse option is of course to use the JSON as text only and check for the value of code and description using XSLT String functions, otherwise you need to transform the JSON data into JSONx that can be handled in XSLT by using a convert of Query Params to XML. To do this you need to pass your JSON to a new Action (Convert QP) and then into a new Transform.

    You can then use DP's JSONx to handle any "JSON" data operations you'd like as XML in the XSLT stylesheet and when done call store://jsonx2json.xsl in a new Transform to transform it into "real" JSON.

    I assume you are using a dp:url-open() as you get your response in a variable but if your service has Request/Resoonse as JSON the context VAR __JSONASJSONX will automatically hold a JSONx object for you that you can use as the INPUT to any XSLT Transform action.

    That being said, let's move over to the good solution; use GWS!

    GWS is added to DataPower to handle JSON (and anything other than XML) and you can call a GWS from your XSLT using dp:gatewayscript() as well. See this sample: https://github.com/ibm-datapower/datapower-samples/blob/master/gatewayscript/example-ext-func-dp-gwscript.xsl

    If your Transform is not doing anything XML specific I'd rewrite it into GWS!