Topic
  • 7 replies
  • Latest Post - ‏2013-09-27T05:45:16Z by arun_tcs
arun_tcs
arun_tcs
144 Posts

Pinned topic Determining if message is JSON

‏2013-09-11T15:40:21Z |

Hi ,

I have a MPGW which handles SOAP, XML , non-xml and JSON messages. At the entry point I want to determine the message type so that the processing of the message can be handled accordingly.

Is there any standard way to determine if the incoming message is JSON ?

if not , what would be the work around for this ?

 

Regards,

Arun

 

 

  • kenhygh
    kenhygh
    1613 Posts

    Re: Determining if message is JSON

    ‏2013-09-11T17:35:32Z  

    Any system sending JSON should use Content-Type: application/json.

     

    But not all do :-(

  • Nitin_Jha
    Nitin_Jha
    20 Posts

    Re: Determining if message is JSON

    ‏2013-09-11T20:44:41Z  

    HI Arun

     As suggested above by Ken, check if the value of the  header Content-Type is application/json. You can do it in the match action in the MPG rule.

    or if your service should handle only json, set the request type in your MPG as JSON.

  • HermannSW
    HermannSW
    4885 Posts

    Re: Determining if message is JSON

    ‏2013-09-11T21:02:14Z  

    Hi Arun,

    if you do not get "application/json" content type, you can try this simple solution.

    While JSON text (unicode, "shall") can be encoded in differnt UTF-* encodings
    http://tools.ietf.org/html/rfc4627#section-3

    and JSON is Non-XML -- on DataPower you have to know the input encoding (see slide 4 of [2]).

    For UTF-16LE you would take Sting.UTF-16LE.input.xsl from slide 10 of [1].

    After that, you have a string.

    XML (and SOAP) has to start with '<', after optional preceding whitespace (no whitespace before xml-declaration).

    Similar, by above JSON spec, JSON text has to start with either '{' or '[', after optional preceding whitespace.

    So your test can be something like:

    <!-- eliminate preceding whitespace for test -->
    <xsl:variable name="aux" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="starts-with($aux,'[') or starts-with($aux,'{')"> ... possible JSON ... </xsl:when>
      <xsl:when test="starts-with($aux,'<')"> ... possible XML... </xsl:when>
      <xsl:otherwise> ... possible Non-XML... </xsl:otherwise>
    </xsl:choose>


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

     

  • souciance
    souciance
    203 Posts

    Re: Determining if message is JSON

    ‏2013-09-12T08:04:26Z  

    Try converting your message to JSONX and if it fails then it is an invalid JSON message?

  • Nitin_Jha
    Nitin_Jha
    20 Posts

    Re: Determining if message is JSON

    ‏2013-09-12T21:34:01Z  
    • HermannSW
    • ‏2013-09-11T21:02:14Z

    Hi Arun,

    if you do not get "application/json" content type, you can try this simple solution.

    While JSON text (unicode, "shall") can be encoded in differnt UTF-* encodings
    http://tools.ietf.org/html/rfc4627#section-3

    and JSON is Non-XML -- on DataPower you have to know the input encoding (see slide 4 of [2]).

    For UTF-16LE you would take Sting.UTF-16LE.input.xsl from slide 10 of [1].

    After that, you have a string.

    XML (and SOAP) has to start with '<', after optional preceding whitespace (no whitespace before xml-declaration).

    Similar, by above JSON spec, JSON text has to start with either '{' or '[', after optional preceding whitespace.

    So your test can be something like:

    <!-- eliminate preceding whitespace for test -->
    <xsl:variable name="aux" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="starts-with($aux,'[') or starts-with($aux,'{')"> ... possible JSON ... </xsl:when>
      <xsl:when test="starts-with($aux,'<')"> ... possible XML... </xsl:when>
      <xsl:otherwise> ... possible Non-XML... </xsl:otherwise>
    </xsl:choose>


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

     

    Nice crude solution Herman. :) Did not think of this.

  • arun_tcs
    arun_tcs
    144 Posts

    Re: Determining if message is JSON

    ‏2013-09-27T04:04:44Z  
    • HermannSW
    • ‏2013-09-11T21:02:14Z

    Hi Arun,

    if you do not get "application/json" content type, you can try this simple solution.

    While JSON text (unicode, "shall") can be encoded in differnt UTF-* encodings
    http://tools.ietf.org/html/rfc4627#section-3

    and JSON is Non-XML -- on DataPower you have to know the input encoding (see slide 4 of [2]).

    For UTF-16LE you would take Sting.UTF-16LE.input.xsl from slide 10 of [1].

    After that, you have a string.

    XML (and SOAP) has to start with '<', after optional preceding whitespace (no whitespace before xml-declaration).

    Similar, by above JSON spec, JSON text has to start with either '{' or '[', after optional preceding whitespace.

    So your test can be something like:

    <!-- eliminate preceding whitespace for test -->
    <xsl:variable name="aux" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="starts-with($aux,'[') or starts-with($aux,'{')"> ... possible JSON ... </xsl:when>
      <xsl:when test="starts-with($aux,'<')"> ... possible XML... </xsl:when>
      <xsl:otherwise> ... possible Non-XML... </xsl:otherwise>
    </xsl:choose>


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

     

    Hi Herman,

    As per your suggestion I used ffd to convert the incoming message into string and was able to check if the message is SOAP,XML or JSON.

    In the  later part of the processing , i need to parse the message based on its type.

    After this step , I am seeing that the binnary transformation is not converting the string back to nodeset  and my xslt in the next step is failing with the following reason -

    Incomplete markup or missing document element at offset 0

    following code is used to convert the message into String and then back to orignal -

    <dp:input-mappingtype="ffd"href="String.utf-8.ffd"/>

    <dp:output-mappinghref="String.utf-8.ffd"type="ffd"/>

    Regards,

    Arun

     

  • arun_tcs
    arun_tcs
    144 Posts

    Re: Determining if message is JSON

    ‏2013-09-27T05:45:16Z  
    • arun_tcs
    • ‏2013-09-27T04:04:44Z

    Hi Herman,

    As per your suggestion I used ffd to convert the incoming message into string and was able to check if the message is SOAP,XML or JSON.

    In the  later part of the processing , i need to parse the message based on its type.

    After this step , I am seeing that the binnary transformation is not converting the string back to nodeset  and my xslt in the next step is failing with the following reason -

    Incomplete markup or missing document element at offset 0

    following code is used to convert the message into String and then back to orignal -

    <dp:input-mappingtype="ffd"href="String.utf-8.ffd"/>

    <dp:output-mappinghref="String.utf-8.ffd"type="ffd"/>

    Regards,

    Arun

     

    I was able to fix the issue. below is the code -

     

    <xsl:copy-of select="dp:parse(/object/message)"/>