Topic
  • 9 replies
  • Latest Post - ‏2013-07-04T12:40:05Z by HermannSW
rdp87
rdp87
45 Posts

Pinned topic Need Help With XML to Json Transformation

‏2013-06-28T16:15:50Z |

Hi All,

Need small help with XML to JSON transformation. My input is like as mentioned below:

Input:

<response>
   <ver>2.0</ver>
   <source>31<source>
   <data>
      <table>24<table>
      <batch>40001<batch>
   </data>
   <data>
      <table>25<table>
      <batch>40002<batch>
   </data>
</response>

 

and i need output as

{
"response":{
 "ver": 2.0,
 "source":31,
 "data": [
   {
    "table": 24,
    "batch": "40001",
  
   }, {
    "table": 25,
    "batch": "40002",
   
   }
  ]
 }
}

 

though the output i am getting with my xsl is having data field and square braces come again and again. please help here.

Thanks.....

  • Rohit-Goyal
    Rohit-Goyal
    139 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-06-29T00:56:21Z  

    You need to convert your XML to JSONX and then use store:///jsonxtojson.xsl to convert to JSON.

    Input -> Transform (XML to JSONX) -> Transform (JSONX to JSON) -> Result

    Now how the JSONX will look like that something you need to explore. One way backward approach. Take JSON that you want as result and convert into JSONX. JSON to JSONX conversion is easy and DataPower gives out of box method. I tried to capture the method in this blog entry.

    https://www.ibm.com/developerworks/community/blogs/datapower-bits/entry/working_with_json_in_datapower?lang=en

    Rohit

     

  • HermannSW
    HermannSW
    4903 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-06-29T09:27:37Z  

    You need to convert your XML to JSONX and then use store:///jsonxtojson.xsl to convert to JSON.

    Input -> Transform (XML to JSONX) -> Transform (JSONX to JSON) -> Result

    Now how the JSONX will look like that something you need to explore. One way backward approach. Take JSON that you want as result and convert into JSONX. JSON to JSONX conversion is easy and DataPower gives out of box method. I tried to capture the method in this blog entry.

    https://www.ibm.com/developerworks/community/blogs/datapower-bits/entry/working_with_json_in_datapower?lang=en

    Rohit

     

    You can create a stylesheet that does directly transform your XML input to JSON.
    But then you wll have to implement all the escaping and other rules from JSON spec http://www.ietf.org/rfc/rfc4627.txt.
     

    Or, as Rohit described, you can go the "XML-->JSONX-->JSON" path, which I will describe here.

    First, determine what the JSONX needs to look like by sending your output to a JSON request type service with
    match(all) and __JSONASJSONX--Results--OUTPUT action:
     

    $ curl --data-binary @rdp87.json http://firestar:2057 -s | tidy -q -xml
    <?xml version="1.0" encoding="utf-8"?>
    <json:object
    xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
      <json:object name="response">
        <json:number name="ver">2.0</json:number>
        <json:number name="source">31</json:number>
        <json:array name="data">
          <json:object>
            <json:number name="table">24</json:number>
            <json:string name="batch">40001</json:string>
          </json:object>
          <json:object>
            <json:number name="table">25</json:number>
            <json:string name="batch">40002</json:string>
          </json:object>
        </json:array>
      </json:object>
    </json:object>

    $

     

    And here is a transform stylesheet converting your input XML to the same JSONX:

    $ cat rdp87.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"
      xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
    >
      <xsl:template match="/response">
        <json:object
          xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
        >
          <json:object name="response">
            <xsl:apply-templates select="ver|source" mode="number"/>

            <json:array name="data">
              <xsl:apply-templates select="data"/>
            </json:array>
          </json:object>
        </json:object>
      </xsl:template>
     
      <xsl:template match="data">
        <json:object>
          <xsl:apply-templates select="table" mode="number"/>
          <xsl:apply-templates select="batch" mode="string"/>
        </json:object>
      </xsl:template>
     
      <xsl:template match="*" mode="number">
        <json:number name="{name()}"><xsl:value-of select="."/></json:number>
      </xsl:template>
     
      <xsl:template match="*" mode="string">
        <json:string name="{name()}"><xsl:value-of select="."/></json:string>
      </xsl:template>
    </xsl:stylesheet>
    $

     

    Some templates are generic (for mode "number" and mode "string"), others are sprecific for your application.

    So finally take the output of above stylesheet and transform it with "store:///jsonxtojson.xsl" to the JSON you need.

     

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

    Updated on 2013-06-29T09:30:18Z at 2013-06-29T09:30:18Z by HermannSW
  • Skumar87
    Skumar87
    9 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-06-29T10:31:44Z  
    • HermannSW
    • ‏2013-06-29T09:27:37Z

    You can create a stylesheet that does directly transform your XML input to JSON.
    But then you wll have to implement all the escaping and other rules from JSON spec http://www.ietf.org/rfc/rfc4627.txt.
     

    Or, as Rohit described, you can go the "XML-->JSONX-->JSON" path, which I will describe here.

    First, determine what the JSONX needs to look like by sending your output to a JSON request type service with
    match(all) and __JSONASJSONX--Results--OUTPUT action:
     

    $ curl --data-binary @rdp87.json http://firestar:2057 -s | tidy -q -xml
    <?xml version="1.0" encoding="utf-8"?>
    <json:object
    xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
      <json:object name="response">
        <json:number name="ver">2.0</json:number>
        <json:number name="source">31</json:number>
        <json:array name="data">
          <json:object>
            <json:number name="table">24</json:number>
            <json:string name="batch">40001</json:string>
          </json:object>
          <json:object>
            <json:number name="table">25</json:number>
            <json:string name="batch">40002</json:string>
          </json:object>
        </json:array>
      </json:object>
    </json:object>

    $

     

    And here is a transform stylesheet converting your input XML to the same JSONX:

    $ cat rdp87.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"
      xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
    >
      <xsl:template match="/response">
        <json:object
          xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
        >
          <json:object name="response">
            <xsl:apply-templates select="ver|source" mode="number"/>

            <json:array name="data">
              <xsl:apply-templates select="data"/>
            </json:array>
          </json:object>
        </json:object>
      </xsl:template>
     
      <xsl:template match="data">
        <json:object>
          <xsl:apply-templates select="table" mode="number"/>
          <xsl:apply-templates select="batch" mode="string"/>
        </json:object>
      </xsl:template>
     
      <xsl:template match="*" mode="number">
        <json:number name="{name()}"><xsl:value-of select="."/></json:number>
      </xsl:template>
     
      <xsl:template match="*" mode="string">
        <json:string name="{name()}"><xsl:value-of select="."/></json:string>
      </xsl:template>
    </xsl:stylesheet>
    $

     

    Some templates are generic (for mode "number" and mode "string"), others are sprecific for your application.

    So finally take the output of above stylesheet and transform it with "store:///jsonxtojson.xsl" to the JSON you need.

     

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

    Hi Herman and Rohit,

    We have a customized xsl to convert json to xml. But when i do it i am getting the response as follows,

    [{

    "response" : {

    "ver" : "2.0",

    "source" : "31",

    "data" : {

     "table" : "24",

    "batch" : "40001"

    },

    "data" : {

     "table" : "25",

    "batch" : "40002"

    }

    }}]

     

    Could you please help me in fine tuning this xsl, so that  the braces ("[" and "]") comes only inside the "data" element. Also "data" should be printed only once...   I am attaching the xsl for your reference..    I am also pasting it below...

    XSL to CONVERT XML TO JSON

     

    <?xml version="1.0"?>

    <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:outputmethod="text"/>

     

     

    <xsl:templatematch="/">[{

    <xsl:apply-templatesselect="*"/>}]

    </xsl:template>

     

     

    <!--Object or Element Property-->

    <xsl:templatematch="*">

    <xsl:iftest=". != ''">

    <xsl:choose>

    <xsl:whentest="string-length(substring-after(name(), ':' )) &gt; 0">

    "<xsl:value-ofselect="substring-after( name(), ':' )"/>" : <xsl:call-templatename="Properties"/>

    </xsl:when>

    <xsl:otherwise>

    "<xsl:value-ofselect="name()"/>" : <xsl:call-templatename="Properties"/>

     

     

    </xsl:otherwise>

    </xsl:choose>

    </xsl:if>

    </xsl:template>

     

     

    <!--Array Element -->

    <xsl:templatematch="*"mode="ArrayElement">

    <xsl:call-templatename="Properties"/>

    </xsl:template>

     

     

    <!--Object Properties -->

    <xsl:templatename="Properties">

    <xsl:variablename="childName"select="name(*[1])"/>

    <xsl:choose>

    <xsl:whentest="not(*|@*)">"<xsl:value-ofselect="."/>"</xsl:when>

    <xsl:whentest="count(*[name()=$childName]) > 1">{ "<xsl:value-ofselect="$childName"/>" :[<xsl:apply-templatesselect="*"mode="ArrayElement"/>] }</xsl:when>

    <xsl:otherwise>{

    <xsl:apply-templatesselect="@*"/>

    <xsl:apply-templatesselect="*"/>

    }</xsl:otherwise>

    </xsl:choose>

     

    <xsl:iftest="following-sibling::* !='' ">,</xsl:if>

     

    </xsl:template>

     

     

    <!--Attribute Property -->

    <xsl:templatematch="@*">"<xsl:value-ofselect="name()"/>" : "<xsl:value-ofselect="."/>",

    </xsl:template>

    </xsl:stylesheet>

     

     

     

    Attachments

  • HermannSW
    HermannSW
    4903 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-06-29T15:35:50Z  
    • Skumar87
    • ‏2013-06-29T10:31:44Z

    Hi Herman and Rohit,

    We have a customized xsl to convert json to xml. But when i do it i am getting the response as follows,

    [{

    "response" : {

    "ver" : "2.0",

    "source" : "31",

    "data" : {

     "table" : "24",

    "batch" : "40001"

    },

    "data" : {

     "table" : "25",

    "batch" : "40002"

    }

    }}]

     

    Could you please help me in fine tuning this xsl, so that  the braces ("[" and "]") comes only inside the "data" element. Also "data" should be printed only once...   I am attaching the xsl for your reference..    I am also pasting it below...

    XSL to CONVERT XML TO JSON

     

    <?xml version="1.0"?>

    <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:outputmethod="text"/>

     

     

    <xsl:templatematch="/">[{

    <xsl:apply-templatesselect="*"/>}]

    </xsl:template>

     

     

    <!--Object or Element Property-->

    <xsl:templatematch="*">

    <xsl:iftest=". != ''">

    <xsl:choose>

    <xsl:whentest="string-length(substring-after(name(), ':' )) &gt; 0">

    "<xsl:value-ofselect="substring-after( name(), ':' )"/>" : <xsl:call-templatename="Properties"/>

    </xsl:when>

    <xsl:otherwise>

    "<xsl:value-ofselect="name()"/>" : <xsl:call-templatename="Properties"/>

     

     

    </xsl:otherwise>

    </xsl:choose>

    </xsl:if>

    </xsl:template>

     

     

    <!--Array Element -->

    <xsl:templatematch="*"mode="ArrayElement">

    <xsl:call-templatename="Properties"/>

    </xsl:template>

     

     

    <!--Object Properties -->

    <xsl:templatename="Properties">

    <xsl:variablename="childName"select="name(*[1])"/>

    <xsl:choose>

    <xsl:whentest="not(*|@*)">"<xsl:value-ofselect="."/>"</xsl:when>

    <xsl:whentest="count(*[name()=$childName]) > 1">{ "<xsl:value-ofselect="$childName"/>" :[<xsl:apply-templatesselect="*"mode="ArrayElement"/>] }</xsl:when>

    <xsl:otherwise>{

    <xsl:apply-templatesselect="@*"/>

    <xsl:apply-templatesselect="*"/>

    }</xsl:otherwise>

    </xsl:choose>

     

    <xsl:iftest="following-sibling::* !='' ">,</xsl:if>

     

    </xsl:template>

     

     

    <!--Attribute Property -->

    <xsl:templatematch="@*">"<xsl:value-ofselect="name()"/>" : "<xsl:value-ofselect="."/>",

    </xsl:template>

    </xsl:stylesheet>

     

     

     

    Please don't do this the way you tried,

    You did wrong output for numbers, numbers do not need quotation marks like string.

    Also you did not do anything for the mentioned spec required needed escaping of JSON chararcters
    (quote, reverse solidus and all control characters need to be escaped).

    Find below stylesheet rdp82.2.xsl (also attached) which does the whole transformationfor you.
    It first does the XML-->JSONX transformation from last posting and stores the result in $jsonx.
    Finally "dp:transform('store:///jsonx2json.xsl', $jsonx)" returns the JSON output you need::

    $ coproc2 rdp87.2.xsl rdp87.xml http://dp2-l3:2223 ; echo
    { "response":{ "ver":2.0, "source":31, "data":[ { "table":24, "batch":"40001" }, { "table":25, "batch":"40002" } ] } }
    $
    $ cat rdp87.2.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"
      xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
    >
      <xsl:template match="/response">
        <xsl:variable name="jsonx">
          <json:object
            xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
          >
            <json:object name="response">
              <xsl:apply-templates select="ver|source" mode="number"/>

              <json:array name="data">
                <xsl:apply-templates select="data"/>
              </json:array>
            </json:object>
          </json:object>
        </xsl:variable>

        <xsl:copy-of select="dp:transform('store:///jsonx2json.xsl', $jsonx)"/>
      </xsl:template>
     
      <xsl:template match="data">
        <json:object>
          <xsl:apply-templates select="table" mode="number"/>
          <xsl:apply-templates select="batch" mode="string"/>
        </json:object>
      </xsl:template>
     
      <xsl:template match="*" mode="number">
        <json:number name="{name()}"><xsl:value-of select="."/></json:number>
      </xsl:template>
     
      <xsl:template match="*" mode="string">
        <json:string name="{name()}"><xsl:value-of select="."/></json:string>
      </xsl:template>
    </xsl:stylesheet>
    $


    Another important aspect of XML-->JSON escaping is performnce.

    In May fixpack I did fix APAR IC90781: POOR PERFORMANCE ON JSONX TO JSON CONVERSION
    http://www-01.ibm.com/support/docview.wss?uid=swg1IC90781

    Any purely stylesheet based solution cannot achieve the same performance as store:///jsonx2json.xsl after the APAR fix:.

    ...
    Performance for <json:string> conversion was poor due to several
    regexp:replace() statements needed to handle JSON escaping. The
    escaping functionality is now realized in firmware code.
    ...


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

    Attachments

    Updated on 2013-06-29T15:44:19Z at 2013-06-29T15:44:19Z by HermannSW
  • Skumar87
    Skumar87
    9 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-06-30T11:13:02Z  

    @hermannSW Hi Herman, I do agree to your point. But the fact is, the response inside the data array may vary, i mean based on the inputs there could be new field names or additional filed names coming as part of response. Similarily there could be many more responses... Hence we are trying to find a generic xsl to convert this, thats the reason i am asking for your help regarding this...   I have still more tweaked my xsl, but i am getting the data column repeated..  We are trying to get one generic xsl for many kind of responses... I would appreciate your help on this.

    CURRENT OUTPUT FROM XSL

    {

    "response" :

    {

     

    "ver" : 2.0" ,

    "source" : 31" ,

    "data" :

    [ {

    "table" : 24" ,

    "batch" : 40001" }]

    ,

    "data" :

    [ {

    "table" : 25" ,

    "batch" : 40002" }]

     

    }

    }

     

    REQUIRED OUTPUT

     

    {
    "response":{
     "ver": 2.0,
     "source":31,
     "data": [
       {
        "table": 24,
        "batch": "40001",
      
       }, {
        "table": 25,
        "batch": "40002",
       
       }
      ]
     }
    }

     

     

     

    SOURCE XSL BEING USED

    <?xml version="1.0"?>

    <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:outputmethod="text"/>

    <xsl:variablename="outermostElementName"select="name(/*)"/>

    <xsl:templatematch="/">{<xsl:apply-templatesselect="*"/>}

    </xsl:template>

    <xsl:templatematch="*">

    <xsl:iftest=". != ''">

    <xsl:choose>

    <xsl:whentest="string-length(substring-after(name(), ':' )) &gt; 0">

    "<xsl:value-ofselect="substring-after( name(), ':' )"/>" : <xsl:call-templatename="properties"/>

    </xsl:when>

    <xsl:otherwise>

    "<xsl:value-ofselect="name()"/>" : <xsl:call-templatename="properties"/>

    </xsl:otherwise>

    </xsl:choose>

    </xsl:if>

    </xsl:template>

    <xsl:templatematch="*"mode="ArrayElement">

    <xsl:call-templatename="properties"/>

    </xsl:template>

    <xsl:templatename="properties1">

    [ { <xsl:apply-templatesselect="*"></xsl:apply-templates>}]

    </xsl:template>

    <xsl:templatename="properties">

    <xsl:variablename="child"select="name(*[1])"></xsl:variable>

    <xsl:choose>

    <xsl:whentest="count(child::*) &gt;= 1 and name()!=$outermostElementName">

    <xsl:call-templatename="properties1"></xsl:call-template>

    </xsl:when>

    <xsl:whentest="not(*|@*)"><xsl:value-ofselect="."/>" </xsl:when>

    <xsl:otherwise>

    {

    <xsl:apply-templatesselect="@*"/>

    <xsl:apply-templatesselect="*"/>

    }

    </xsl:otherwise>

     

    </xsl:choose>

    <xsl:iftest="following-sibling::* !='' ">,</xsl:if>

    </xsl:template>

    <xsl:templatematch="@*">"<xsl:value-ofselect="name()"/>" : "<xsl:value-ofselect="."/>",</xsl:template>

    </xsl:stylesheet>

     

  • HermannSW
    HermannSW
    4903 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-06-30T12:50:04Z  
    • Skumar87
    • ‏2013-06-30T11:13:02Z

    @hermannSW Hi Herman, I do agree to your point. But the fact is, the response inside the data array may vary, i mean based on the inputs there could be new field names or additional filed names coming as part of response. Similarily there could be many more responses... Hence we are trying to find a generic xsl to convert this, thats the reason i am asking for your help regarding this...   I have still more tweaked my xsl, but i am getting the data column repeated..  We are trying to get one generic xsl for many kind of responses... I would appreciate your help on this.

    CURRENT OUTPUT FROM XSL

    {

    "response" :

    {

     

    "ver" : 2.0" ,

    "source" : 31" ,

    "data" :

    [ {

    "table" : 24" ,

    "batch" : 40001" }]

    ,

    "data" :

    [ {

    "table" : 25" ,

    "batch" : 40002" }]

     

    }

    }

     

    REQUIRED OUTPUT

     

    {
    "response":{
     "ver": 2.0,
     "source":31,
     "data": [
       {
        "table": 24,
        "batch": "40001",
      
       }, {
        "table": 25,
        "batch": "40002",
       
       }
      ]
     }
    }

     

     

     

    SOURCE XSL BEING USED

    <?xml version="1.0"?>

    <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:outputmethod="text"/>

    <xsl:variablename="outermostElementName"select="name(/*)"/>

    <xsl:templatematch="/">{<xsl:apply-templatesselect="*"/>}

    </xsl:template>

    <xsl:templatematch="*">

    <xsl:iftest=". != ''">

    <xsl:choose>

    <xsl:whentest="string-length(substring-after(name(), ':' )) &gt; 0">

    "<xsl:value-ofselect="substring-after( name(), ':' )"/>" : <xsl:call-templatename="properties"/>

    </xsl:when>

    <xsl:otherwise>

    "<xsl:value-ofselect="name()"/>" : <xsl:call-templatename="properties"/>

    </xsl:otherwise>

    </xsl:choose>

    </xsl:if>

    </xsl:template>

    <xsl:templatematch="*"mode="ArrayElement">

    <xsl:call-templatename="properties"/>

    </xsl:template>

    <xsl:templatename="properties1">

    [ { <xsl:apply-templatesselect="*"></xsl:apply-templates>}]

    </xsl:template>

    <xsl:templatename="properties">

    <xsl:variablename="child"select="name(*[1])"></xsl:variable>

    <xsl:choose>

    <xsl:whentest="count(child::*) &gt;= 1 and name()!=$outermostElementName">

    <xsl:call-templatename="properties1"></xsl:call-template>

    </xsl:when>

    <xsl:whentest="not(*|@*)"><xsl:value-ofselect="."/>" </xsl:when>

    <xsl:otherwise>

    {

    <xsl:apply-templatesselect="@*"/>

    <xsl:apply-templatesselect="*"/>

    }

    </xsl:otherwise>

     

    </xsl:choose>

    <xsl:iftest="following-sibling::* !='' ">,</xsl:if>

    </xsl:template>

    <xsl:templatematch="@*">"<xsl:value-ofselect="name()"/>" : "<xsl:value-ofselect="."/>",</xsl:template>

    </xsl:stylesheet>

     

    Sorry, I had this discussion with others before.

    "store:///jsonx2json.xsl" is generic.
    But only because there <json:array> elements exactly define where arrays are, and where single elements.

    You cannot tell generically whether <a><b>1</b><b>2</b><b>3</b></a> is an array of 3 b's, a b with an array of 2 b's, ...

    Also your approach cannot be generic with the difference with numbers (no quotes) and strings (quotes).
    For JSONX there is no problem because of <json:number> and <json:string> elements.

    You need to work on your input format and/or define strict rules, otherwise there is no chance for a generic solution.
    Without that any effort is hopeless.


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

  • rdp87
    rdp87
    45 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-07-01T09:45:18Z  
    • HermannSW
    • ‏2013-06-30T12:50:04Z

    Sorry, I had this discussion with others before.

    "store:///jsonx2json.xsl" is generic.
    But only because there <json:array> elements exactly define where arrays are, and where single elements.

    You cannot tell generically whether <a><b>1</b><b>2</b><b>3</b></a> is an array of 3 b's, a b with an array of 2 b's, ...

    Also your approach cannot be generic with the difference with numbers (no quotes) and strings (quotes).
    For JSONX there is no problem because of <json:number> and <json:string> elements.

    You need to work on your input format and/or define strict rules, otherwise there is no chance for a generic solution.
    Without that any effort is hopeless.


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

    Hi Hermann,

    With my current xsl is am able to get the respose close to my need after modifying my input like as mentioned below.

    <response>
       <ver>2.0</ver>
       <source>31<source>
       <data>
          <table>24<table>
          <batch>40001<batch>
           <table>25<table>
          <batch>40002<batch>
       </data>
    </response>

    But  I am not able to transform my xml like this using xsl transformation. except Data tag everything is coming. Any way to achieve this.

    Thanks....

  • HermannSW
    HermannSW
    4903 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-07-01T10:42:15Z  
    • rdp87
    • ‏2013-07-01T09:45:18Z

    Hi Hermann,

    With my current xsl is am able to get the respose close to my need after modifying my input like as mentioned below.

    <response>
       <ver>2.0</ver>
       <source>31<source>
       <data>
          <table>24<table>
          <batch>40001<batch>
           <table>25<table>
          <batch>40002<batch>
       </data>
    </response>

    But  I am not able to transform my xml like this using xsl transformation. except Data tag everything is coming. Any way to achieve this.

    Thanks....

    Hi,

    you did not specifiy the rules for your generic transformation.

    How can a JSON object like "response" in your modified XML be distinguished from a JSON array like "data"?

    How should a JSON array deal with different elements like "table" and "batch"?

    Without a deterministic format and a deterministic set of rules you cannot get a generic conversion.
    And a conversion with numbers being output as strings is not good.


    So for a generic stylesheet you have quite some specification effort left.
    For a non-generic stylesheet please use the one from this posting.


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

    Updated on 2013-07-01T10:44:08Z at 2013-07-01T10:44:08Z by HermannSW
  • HermannSW
    HermannSW
    4903 Posts

    Re: Need Help With XML to Json Transformation

    ‏2013-07-04T12:40:05Z  
    • HermannSW
    • ‏2013-07-01T10:42:15Z

    Hi,

    you did not specifiy the rules for your generic transformation.

    How can a JSON object like "response" in your modified XML be distinguished from a JSON array like "data"?

    How should a JSON array deal with different elements like "table" and "batch"?

    Without a deterministic format and a deterministic set of rules you cannot get a generic conversion.
    And a conversion with numbers being output as strings is not good.


    So for a generic stylesheet you have quite some specification effort left.
    For a non-generic stylesheet please use the one from this posting.


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

    Posting "XML to JSON Transformation" contains a direct "JSON->XML" conversion (rdp87.3.xsl) as well as comparison to "JSON->JSONX->XML" solution(s):
    https://www.ibm.com/developerworks/community/blogs/HermannSW/entry/xml_to_json_transformation


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