user5458829
user5458829

Reputation: 163

How to extract data from JSON in XSLT?

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.

Upvotes: 2

Views: 8929

Answers (2)

Manoj K
Manoj K

Reputation: 28

<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.

Upvotes: 0

kjhughes
kjhughes

Reputation: 111620

XSLT 3.0 / XPath 3.1

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

Upvotes: 3

Related Questions