Topic
1 reply Latest Post - ‏2013-06-26T21:06:34Z by HermannSW
RubenRandall
RubenRandall
85 Posts
ACCEPTED ANSWER

Pinned topic record Json in context variable

‏2013-06-26T17:56:35Z |

Hi ,

 

I am trying to figure out a way by which the raw incoming JSON message is stored it in a context variable, could any one give me a sample stylesheet on it

Thanks,

Ruben

  • HermannSW
    HermannSW
    4360 Posts
    ACCEPTED ANSWER

    Re: record Json in context variable

    ‏2013-06-26T21:06:34Z  in response to RubenRandall

    The easiest solution to that is just to set request type to JSON.

    With that the converted JSONX is stored in context "__JSONASJSONX".
    And you can access the whole context by "dp:variable('var://context/__JSONASJSONX')".

    Now if you want the JSON string you will get it with Transform Binary action referencing "to-utf8.xsl" stylesheet.
    Just use <dp:set-variable> to fill the context variable you want.

    If you want only the JSON string, please use request-type Non-XML because that avoids that JSON to JSONX conversion.

    $ cat to-utf8.xsl
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:dp="http://www.datapower.com/extensions"
      extension-element-prefixes="dp"
    >
      <dp:input-mapping  href="store:///pkcs7-convert-input.ffd" type="ffd"/>

      <xsl:output omit-xml-declaration="yes" />
        
      <xsl:template match="/">
        <xsl:variable name="input64"
          select="dp:binary-encode(/object/message/node())"
        />
        <!-- this does UTF-8 validation and decoding -->
        <xsl:variable name="str" select="dp:decode($input64, 'base-64')"/>

        <read>
          <xsl:value-of select="$str"/>
        </read>
      </xsl:template>
     
    </xsl:stylesheet>
    $

     

    Hermann<myXsltBlog/> <myXsltTweets/> <myCE/>