Topic
16 replies Latest Post - ‏2013-11-23T00:07:32Z by HermannSW
Daviiid
Daviiid
215 Posts
ACCEPTED ANSWER

Pinned topic url rewrite json response

‏2013-09-25T08:14:40Z |

Hello all

I have this response

{
  "links" : [ {
    "rel" : "self",
    "href" : "http://s73242vw:28080/restSGBDAdapter/carnet/28492"
  } ],
  "prenom" : "Philippe",
  "categorie" : "Directions régionales",
  "service" : "CENTRE POITOU CHARENTES",
  "fonction" : "Directeur Régional",
  "telephone" : "",
  "nom" : "DE RANCOURT",
  "mobile" : "06.67.48.54.74"
}

What is the best way to change a value (http://s73242vw:28080 for example)

Do i need a styleSheet or a url rewrite is enough ?

  • HermannSW
    HermannSW
    2817 Posts
    ACCEPTED ANSWER

    Re: url rewrite json response

    ‏2013-09-25T08:23:37Z  in response to Daviiid

    Hi David,

    which firmware version do you need this for?

    With v6.0 this can be done completely without stylesheet by a JSONiq transaform.
    http://pic.dhe.ibm.com/infocenter/wsdatap/v6r0m0/index.jsp?topic=%2Fcom.ibm.dp.xi.doc%2Fjson_jsontransformationexample3.html


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

    • Daviiid
      Daviiid
      215 Posts
      ACCEPTED ANSWER

      Re: url rewrite json response

      ‏2013-09-25T08:25:55Z  in response to HermannSW

      Hello Hermann

      In production, we use firmware 5.0. But we have just upgrade in v6.0 in dev..

    • Daviiid
      Daviiid
      215 Posts
      ACCEPTED ANSWER

      Re: url rewrite json response

      ‏2013-09-25T10:06:51Z  in response to HermannSW

      Sorry hermann

      But i don't understand how work transformation with XQuery

      Is it possible to export a MPG or something else with a little example ?

      You will be very helpfull

      • HermannSW
        HermannSW
        2817 Posts
        ACCEPTED ANSWER

        Re: url rewrite json response

        ‏2013-09-25T16:43:57Z  in response to Daviiid

        Hi David,

        the JSONiq equivalent to identity transform stylesheet is not that easy, I will provide later.

        This is how you can do it on pre v6.0 firmwares as well as on v6.0.

        Create a MPGW or XML FW service

        • with request type JSON
        • with below (and attached) transform action from __JSONASJSONX to PIPE
        • with final transform action "store:///jsonx2json.xsl"


        This stylesheet demonstrates the replacement of the href you requested as sample.
        The trick is that after conversion still to have JSONX to allow conversion to JSON by "store:///jsonx2json.xsl".

        <xsl:stylesheet version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
        >
          <xsl:template match="/json:object/json:array/json:object/json:string[@name='href']">
            <xsl:copy>
              <xsl:text>http://1.2.3.4</xsl:text>
              <xsl:value-of select="substring-after(., 'http://s73242vw:28080')"/>
            </xsl:copy>
          </xsl:template>

          <xsl:template match="*">
            <xsl:copy>
              <xsl:copy-of select="@*|namespace::*"/>

              <xsl:apply-templates select="node()" />
            </xsl:copy>
          </xsl:template>

        </xsl:stylesheet>

         

        This is the output generated with your (attached) JSON input file:

        { "links":[ { "rel":"self", "http://1.2.3.4/restSGBDAdapter/carnet/28492" } ], "prenom":"Philippe", "categorie":"Directions régionales", "service":"CENTRE POITOU CHARENTES", "fonction":"Directeur Régional", "telephone":"", "nom":"DE RANCOURT", "mobile":"06.67.48.54.74" }


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

        Attachments

        • Daviiid
          Daviiid
          215 Posts
          ACCEPTED ANSWER

          Re: url rewrite json response

          ‏2013-09-26T09:07:51Z  in response to HermannSW

          Hermann

          I tried your solution and it works. 

          I implemented your solution excepted that i did it on response rule (because the backEnd replies with Json Format). The transformation is correct but when i try in SoapUi, the response is not parsed..

          When i tried calling the same url with I.E., i didn't see the result because it propose me to download a file containing the response. 

          When i don't use datapower to call backend with soapUi or I.E. the response is well parsed in both cases.

          i don't know if it's important but in the goog cas, the next hex character after '{' is 0d0a. But in your transformation it's '20'

          • HermannSW
            HermannSW
            2817 Posts
            ACCEPTED ANSWER

            Re: url rewrite json response

            ‏2013-09-26T09:30:10Z  in response to Daviiid

            Can you please attach a packet capture or XML file capture of the response here?
            Then we can see whether the response is valid JSON or not (I think it is, jsonx2json.xsl guarantees that).

            Perhaps your client application expects a specific content type (application/json)?
            Can you please compare the content type returned by backend and that returned by DataPower (by packet capture)?

            > i don't know if it's important but in the goog cas, the next hex character after '{' is 0d0a. But in your transformation it's '20'
            >
            JSON  begin-object allows for arbitray whitespace before and after left curly bracket:
            http://tools.ietf.org/html/rfc4627#section-2
            begin-object = ws %x7B ws ; { left curly bracket

             

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

             

            • Daviiid
              Daviiid
              215 Posts
              ACCEPTED ANSWER

              Re: url rewrite json response

              ‏2013-09-26T09:55:26Z  in response to HermannSW

              Hermann

              Please see my attachments 

              Here are responses headers through datapower

              X-Backside-Transport OK OK
              Transfer-Encoding chunked
              Date Thu, 26 Sep 2013 08:53:54 GMT
              #status# HTTP/1.1 200 OK
              X-Client-IP 10.75.97.77
              Connection Keep-Alive
              Content-Type application/json
              Server Apache-Coyote/1.1

              response header of direct back End

              Date Thu, 26 Sep 2013 09:54:13 GMT
              Content-Length 345
              #status# HTTP/1.1 200 OK
              Content-Type application/json
              Server Apache-Coyote/1.1

              Thanks Hermann :)

              • HermannSW
                HermannSW
                2817 Posts
                ACCEPTED ANSWER

                Re: url rewrite json response

                ‏2013-09-26T11:14:38Z  in response to Daviiid

                Hi David,

                I am sorry, my stylesheet did miss to copy the attributes of the replacement <json:string> element.

                This simple diff fixes the issue, new stylesheet attached:

                $ diff Daviid.xsl Daviid.2.xsl
                6a7
                >       <xsl:copy-of select="@*|namespace::*"/>
                $

                This was the previous (incorrect) output:

                { "links":[ { "rel":"self", "http://10.145.9.190:10220/re...

                Now it is

                { "links":[ { "rel":"self", "href":"http://...

                 

                Lesson learned: Passing invalid JSONX to  store:///jsonx2json.xsl  results in invalid JSON output.


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

                 

                Attachments

                Updated on 2013-09-26T11:15:53Z at 2013-09-26T11:15:53Z by HermannSW
                • Daviiid
                  Daviiid
                  215 Posts
                  ACCEPTED ANSWER

                  Re: url rewrite json response

                  ‏2013-09-26T14:09:06Z  in response to HermannSW

                  perfect

                  It works :)

                  I'm waiting for an example of JSONiq transmation now when you have time :)

                   

        • Daviiid
          Daviiid
          215 Posts
          ACCEPTED ANSWER

          Re: url rewrite json response

          ‏2013-10-03T14:19:29Z  in response to HermannSW

          Hello Hermann

          Sorry to ask you this question but did you have time to make an example of JSONiq transformation ? (change an url on response)

          Thanks

          • HermannSW
            HermannSW
            2817 Posts
            ACCEPTED ANSWER

            Re: url rewrite json response

            ‏2013-10-03T21:53:09Z  in response to Daviiid

            Hi David,

            > Sorry to ask you this question but did you have time to make an example of JSONiq transformation ? (change an url on response)
            >
            it's OK that you keep pushing.


            I will do 2 blog postings on JSONiq next week, a baisc one on spec version 0.4.42 DataPower supports, discussion on an online 1.0.x processor with cool features, and even more exciting DataPower news [this posting might give you an idea on what ... ;-)]

            Here you can see the "right" JSONiq solution for your problem I just created today -- unfortunately v6.0 release notes state "Support excludes JSON updates".

            Because of that I created 3 different DataPower solutions which is what the second blog posting will be about.


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



            (late response because ooo, 23rd German Unity Day)

            Updated on 2013-10-03T22:03:30Z at 2013-10-03T22:03:30Z by HermannSW
            • Daviiid
              Daviiid
              215 Posts
              ACCEPTED ANSWER

              Re: url rewrite json response

              ‏2013-10-04T07:11:06Z  in response to HermannSW

              thanks for response. I hope you did not drunk to much during this day :)

              I 'm in hurry to see how implement this JSONiq transformation in datapower

              See you

              • HermannSW
                HermannSW
                2817 Posts
                ACCEPTED ANSWER

                Re: url rewrite json response

                ‏2013-10-04T07:34:18Z  in response to Daviiid

                Hi David,

                I am under pressure with some APAR fixes until beginning of next week.

                So find a solution without comments and explanations below.
                There is more stuff to be done before I can do the blog posts, sorry.

                This is the all-in-one JSONiq solution of the three I have:

                $ cat E.xq
                (: local:function demo on relacing for all "{$key1[*{$key2"
                   prefix $pref by $repl while keeping the rest te same     :)
                declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

                declare option jsoniq-version "0.4.42";
                declare option output:method "json";

                (: some constants :)
                declare variable $key1 := "links";
                declare variable $key2 := "href";
                declare variable $pref := "http://s73242vw:28080";
                declare variable $repl := "http://1.2.3.4:56789";

                (: all-in-one :)
                jn:object(
                  for $k in jn:keys(.)
                    return
                      if ($k != $key1)
                        then { $k : .($k) }
                        else { $k :
                          [
                            for $member in jn:members(.($k))
                              return
                                if (not(starts-with($member($key2), $pref)))
                                  then $member
                                  else  
                                    jn:object(
                                      for $k in jn:keys($member)
                                        return
                                          if ($k != $key2)
                                            then { $k : $member($k) }
                                            else { $k : concat(
                                                          $repl,
                                                          substring-after($member($k), $pref)
                                                        )
                                            }
                                    )
                          ]
                        }
                )
                $


                This is the sample output, find service export attached:

                $ curl --data-binary @Daviid.1.json http://firestar:2499; echo


                {
                  "service":"CENTRE POITOU CHARENTES",
                  "mobile":"06.67.48.54.74",
                  "prenom":"Philippe",
                  "nom":"DE RANCOURT",
                  "fonction":"Directeur Régional",
                  "telephone":"",
                  "categorie":"Directions régionales",
                  "links":[
                    {
                      "rel":"self",
                      "href":"http://1.2.3.4:56789/restSGBDAdapter/carnet/28492"
                    }
                  ]
                }
                $
                $ cat Daviid.1.json
                {
                  "links" : [ {
                    "rel" : "self",
                    "href" : "http://s73242vw:28080/restSGBDAdapter/carnet/28492"
                  } ],
                  "prenom" : "Philippe",
                  "categorie" : "Directions régionales",
                  "service" : "CENTRE POITOU CHARENTES",
                  "fonction" : "Directeur Régional",
                  "telephone" : "",
                  "nom" : "DE RANCOURT",
                  "mobile" : "06.67.48.54.74"
                }
                $


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

                 

                Attachments

                Updated on 2013-10-04T07:50:28Z at 2013-10-04T07:50:28Z by HermannSW
                • Daviiid
                  Daviiid
                  215 Posts
                  ACCEPTED ANSWER

                  Re: url rewrite json response

                  ‏2013-10-04T12:35:58Z  in response to HermannSW

                  thanks Hermann

                  I can close the post

                • HermannSW
                  HermannSW
                  2817 Posts
                  ACCEPTED ANSWER

                  Re: url rewrite json response

                  ‏2013-11-23T00:07:32Z  in response to HermannSW

                  With this today's posting coproc2 service was extened to allow for JSONiq as well as XQuery processing.

                  So JSONiq script E.xq can be processed by coproc2 service now:

                  $ coproc2 E.xq Daviid.1.json http://dp3-l3:2226 ; echo

                  {
                    "service":"CENTRE POITOU CHARENTES",
                    "mobile":"06.67.48.54.74",
                    "prenom":"Philippe",
                    "nom":"DE RANCOURT",
                    "fonction":"Directeur Régional",
                    "telephone":"",
                    "categorie":"Directions régionales",
                    "links":[
                      {
                        "rel":"self",
                        "href":"http://1.2.3.4:56789/restSGBDAdapter/carnet/28492"
                      }
                    ]
                  }
                  $


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

  • Daviiid
    Daviiid
    215 Posts
    ACCEPTED ANSWER

    Re: url rewrite json response

    ‏2013-09-26T08:08:56Z  in response to Daviiid

    Thanks Hermann

    I will test with your xsl.

    I'll close this topic when you'll provide a example of JSONiq transmation