3

I am calling a rest service in XSLT and in return getting JSON response .

How can I extract data from that JSON response using XSLT. Below is the XSLT code for calling the rest service and also given the JSON response. From JSON response I need to extract the values of Cookie1, Cookie2 and Cookie3.

XSLT

<xsl:variable name="result1"> 
   <dp:url-open target="{$abc}" response="binaryNode" 
                resolve-mode="xml" data-type="xml" http-method="post">
   </dp:url-open>
</xsl:variable>

<xsl:variable name="json">
  <xsl:value-of select="dp:decode(dp:binary-encode($result1/result/binary/node()), 
                                  'base-64' )" />
</xsl:variable>

JSON Response:

   {"mapData": 
     {
       "Cookie1": "KlzpP965iBw==",
       "status": "True",
       "Cookie2": "DDGT8mcsuzdMNNQ=",
       "Cookie3": "VERSION_4~mPpYUDcZnoJ0Z"
      }
   }

Please let me know how to do this using XSLT.

user987339
  • 10,519
  • 8
  • 40
  • 45
user5458829
  • 173
  • 1
  • 2
  • 15
  • 1
    This is a duplicate of previous asked question. http://stackoverflow.com/questions/13007280/how-to-convert-json-to-xml-using-xslt – Simon Black Oct 19 '16 at 13:18
  • Unless you use an XSLT 3.0 processor there is no built-in, standardized support, you should first check whether your XSLT processor (DataPower?) has some extension functions or instructions to deal with JSON data. – Martin Honnen Oct 19 '16 at 13:22
  • @SimonBlack: That question is about converting JSON to XML in general. This question is less ambitious: It just wants to know how to extract data from JSON using XSLT. (I've fixed the title to reflect the actual question being asked.) – kjhughes Oct 19 '16 at 13:55

2 Answers2

3

XSLT 3.0 / XPath 3.1

Use fn:parse-json() to return a map, then map:get() to get the values of interest.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 3
    Alternatively use fn:json-to-xml() to convert the JSON to a node tree, and use XPath expressions to select into the node tree. – Michael Kay Oct 19 '16 at 15:38
0
<xsl:variable name="ValidationResponse">
<dp:url-open target="{$url}" 
             response="responsecode-binary" http-method="GET" 
             content-type="application/x-www-form-urlencoded" 
             ssl-proxy="abc" http-headers="$headerValues" />
</xsl:variable> 

<dp:set-variable name="'var://context/sample/valResp'" 
                 value="normalize-space($ValidationResponse)"/>

Then convert the output using convert query params or other XSLT to get the desired output.

rene
  • 41,474
  • 78
  • 114
  • 152
Manoj K
  • 38
  • 1
  • 7