Topic
  • 22 replies
  • Latest Post - ‏2013-01-29T15:16:13Z by kenhygh
SystemAdmin
SystemAdmin
6772 Posts

Pinned topic XML to JSON

‏2013-01-02T15:47:11Z |
Hi,

I was successfully I've the below requirement

1) Request flow: json --> XML
for which I was able to do this in DP
a) json to jsonx using http convert action
b) jsonx to xml and send it to service providers.

The# 1 looks good ( which is the client to Serviceproviders flow ).

2) We get the xml response from the service providers. I'd need to change it to json message and sent it to the clients. ( XML --> JSON )

Please let me know what needs to be done. Can someone share the xsl required for this ?

I see there is one xsl ( jsonxtojson.xsl in store ) should I be utilizing this ?

Thank you!
Updated on 2013-01-29T15:16:13Z at 2013-01-29T15:16:13Z by kenhygh
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-02T15:59:51Z  
    You can follow these steps.
    1. write an xsl to convert xml to jsonx format. use the below link for conversion.
    http://pic.dhe.ibm.com/infocenter/wsdatap/v5r0m0/index.jsp?topic=%2Fcom.ibm.dp.xb.doc%2Fconvertingbetweenjsonandjsonx06.htm
    2. Use the jsonx2json xsl to convert the jsonx output for previous action to Json.
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-02T16:08:23Z  
    You can follow these steps.
    1. write an xsl to convert xml to jsonx format. use the below link for conversion.
    http://pic.dhe.ibm.com/infocenter/wsdatap/v5r0m0/index.jsp?topic=%2Fcom.ibm.dp.xb.doc%2Fconvertingbetweenjsonandjsonx06.htm
    2. Use the jsonx2json xsl to convert the jsonx output for previous action to Json.
    Thank you for the prompt response!

    Can you please share the piece of code for converting xml --> jsonx ? --Thanks!
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-02T16:32:58Z  
    Thank you for the prompt response!

    Can you please share the piece of code for converting xml --> jsonx ? --Thanks!
    <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"
    exclude-result-prefixes="dp">

    <xsl:template match="/">
    <xsl:message dp:priority="debug">*** calling soap2jsonx.xsl ***</xsl:message>

    <json:object>
    <xsl:copy>
    <xsl:apply-templates select="*/*/*" mode="body"/>
    </xsl:copy>
    </json:object>
    </xsl:template>

    <xsl:template match="node()" mode="body">
    <xsl:if test="local-name(.) != ''">
    <json:string>
    <xsl:attribute name="name"><xsl:value-of select="local-name(.)"/></xsl:attribute>
    <xsl:value-of select="normalize-space(./text())"/>
    </json:string>
    </xsl:if>
    <xsl:apply-templates mode="body"/>
    </xsl:template>

    </xsl:stylesheet>
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-03T02:13:02Z  
    • kenhygh
    • ‏2013-01-02T16:32:58Z
    <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"
    exclude-result-prefixes="dp">

    <xsl:template match="/">
    <xsl:message dp:priority="debug">*** calling soap2jsonx.xsl ***</xsl:message>

    <json:object>
    <xsl:copy>
    <xsl:apply-templates select="*/*/*" mode="body"/>
    </xsl:copy>
    </json:object>
    </xsl:template>

    <xsl:template match="node()" mode="body">
    <xsl:if test="local-name(.) != ''">
    <json:string>
    <xsl:attribute name="name"><xsl:value-of select="local-name(.)"/></xsl:attribute>
    <xsl:value-of select="normalize-space(./text())"/>
    </json:string>
    </xsl:if>
    <xsl:apply-templates mode="body"/>
    </xsl:template>

    </xsl:stylesheet>
    http://stackoverflow.com/questions/12892266/convert-xml-to-jsonx-using-xslt

    This link has got some more information about same.

    Thanks
    RG
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-04T12:13:40Z  
    http://stackoverflow.com/questions/12892266/convert-xml-to-jsonx-using-xslt

    This link has got some more information about same.

    Thanks
    RG
    Ken and Rohit, Ty for your response!

    I see that xml is now converted to jsonx --> This part is good.

    After that I've to do JSONX to convert into json. Does the same http-convert action works/applicable here ?

    What should be the action type for jsonx to JSON ( when it comes to json to JSONx I've used Convert Query params to XML ).

    I'm getting the below error:
    convert-http: 'Converting jsonxXml with map json results stored in jsondata' failed: Invalid JSON format
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-04T12:29:57Z  
    Ken and Rohit, Ty for your response!

    I see that xml is now converted to jsonx --> This part is good.

    After that I've to do JSONX to convert into json. Does the same http-convert action works/applicable here ?

    What should be the action type for jsonx to JSON ( when it comes to json to JSONx I've used Convert Query params to XML ).

    I'm getting the below error:
    convert-http: 'Converting jsonxXml with map json results stored in jsondata' failed: Invalid JSON format
    jsonxtojson.xsl in store is what looking for. A transform action with input JSONX, applying jsonxtojson.xsl from store will give you JSON.

    RG
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-04T13:15:34Z  
    jsonxtojson.xsl in store is what looking for. A transform action with input JSONX, applying jsonxtojson.xsl from store will give you JSON.

    RG
    Where was my mind :P. Thanks Rohit Goyal ( RG ) for your prompt response! It worked well.Thanks to you and Ken!
  • Jaango
    Jaango
    267 Posts

    Re: XML to JSON

    ‏2013-01-09T10:53:55Z  
    Where was my mind :P. Thanks Rohit Goyal ( RG ) for your prompt response! It worked well.Thanks to you and Ken!
    How will the below two xmls be different in JSONx, by using the generalised xsl transformation to convert xml to jsonx? I want to know the difference in jsonx from xml, for attributes and elements.
    <soap:Envelope
    xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
    soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
     
    <soap:Body xmlns:m="http://www.example.org/stock">
      <m:GetStockPrice>
        <m:StockName>IBM</m:StockName>
      </m:GetStockPrice>
    </soap:Body>
     
    </soap:Envelope>
    


    <soap:Envelope
    xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
    soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
     
    <soap:Body xmlns:m="http://www.example.org/stock">
      <m:GetStockPrice m:StockName="IBM" />
    </soap:Body>
     
    </soap:Envelope>
    


    The difference between the two is that in the second case element is converted to attribute.
    Updated on 2014-03-25T02:42:32Z at 2014-03-25T02:42:32Z by iron-man
  • samanderson
    samanderson
    172 Posts

    Re: XML to JSON

    ‏2013-01-09T13:01:39Z  
    • kenhygh
    • ‏2013-01-02T16:32:58Z
    <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"
    exclude-result-prefixes="dp">

    <xsl:template match="/">
    <xsl:message dp:priority="debug">*** calling soap2jsonx.xsl ***</xsl:message>

    <json:object>
    <xsl:copy>
    <xsl:apply-templates select="*/*/*" mode="body"/>
    </xsl:copy>
    </json:object>
    </xsl:template>

    <xsl:template match="node()" mode="body">
    <xsl:if test="local-name(.) != ''">
    <json:string>
    <xsl:attribute name="name"><xsl:value-of select="local-name(.)"/></xsl:attribute>
    <xsl:value-of select="normalize-space(./text())"/>
    </json:string>
    </xsl:if>
    <xsl:apply-templates mode="body"/>
    </xsl:template>

    </xsl:stylesheet>
    Thanks Ken. does this work for any xml to Json ?
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-09T15:00:05Z  
    Thanks Ken. does this work for any xml to Json ?
    It assumes SOAP. You'll need to tweak it for XML.

    Ken
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-09T15:00:05Z  
    Thanks Ken. does this work for any xml to Json ?
    It assumes SOAP. You'll need to tweak it for XML.

    Ken
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-25T22:25:08Z  
    Where was my mind :P. Thanks Rohit Goyal ( RG ) for your prompt response! It worked well.Thanks to you and Ken!
    Hi Rohit,

    I am getting "Internal server Error" when tring to do JSONXtoJOSN. I exactly follow the steps you suggested but when i hit request from SOAP UI (this is the only place from where i did test)i am not getting response. I used PIPE as Input and PIPE as output for all the steps.I have created xml firewall and set Request/Response as Non-xml.

    Please help me on this.

    Thanks
    Manish

    Error
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <env:Fault>
    <faultcode>env:Client</faultcode>
    <faultstring>Internal Error</faultstring>
    </env:Fault>
    </env:Body>
    </env:Envelope>
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-26T00:29:52Z  
    Hi Rohit,

    I am getting "Internal server Error" when tring to do JSONXtoJOSN. I exactly follow the steps you suggested but when i hit request from SOAP UI (this is the only place from where i did test)i am not getting response. I used PIPE as Input and PIPE as output for all the steps.I have created xml firewall and set Request/Response as Non-xml.

    Please help me on this.

    Thanks
    Manish

    Error
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <env:Fault>
    <faultcode>env:Client</faultcode>
    <faultstring>Internal Error</faultstring>
    </env:Fault>
    </env:Body>
    </env:Envelope>
    Log files are your friend.

    Ken
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-27T06:23:54Z  
    • kenhygh
    • ‏2013-01-26T00:29:52Z
    Log files are your friend.

    Ken
    Hi Ken,

    I didn't see any thing on log files. If i put any debug message thats getting displayed in log. I am not seeing any output content in probe in last xslt which is store///jsonxtojson.xslt. I am seeing correct parsing of jsonx object which i created before(jsonxtojson.xslt).

    Please explain me in details what could be the issue.

    Thanks
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-27T11:50:50Z  
    Hi Ken,

    I didn't see any thing on log files. If i put any debug message thats getting displayed in log. I am not seeing any output content in probe in last xslt which is store///jsonxtojson.xslt. I am seeing correct parsing of jsonx object which i created before(jsonxtojson.xslt).

    Please explain me in details what could be the issue.

    Thanks
    Well, the most probable problem is that your jsonx is something that jsonxtojson cannot parse.

    Attach to a post your jsonx and I'll take a look.

    Ken
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-28T22:06:08Z  
    • kenhygh
    • ‏2013-01-27T11:50:50Z
    Well, the most probable problem is that your jsonx is something that jsonxtojson cannot parse.

    Attach to a post your jsonx and I'll take a look.

    Ken
    Hi Ken,

    Thanks for quick reply. Please find attached zip file having xslt which i am using to mke a call to WS , which returns the soap message then i read and make it in jsonx , for timebeing i replace that i am using jsonx sample message and pass this to the store//jsonxtojson.xls , but i am not able to see anuthing in output of this xslt.

    Thanks
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-29T00:12:41Z  
    Hi Ken,

    Thanks for quick reply. Please find attached zip file having xslt which i am using to mke a call to WS , which returns the soap message then i read and make it in jsonx , for timebeing i replace that i am using jsonx sample message and pass this to the store//jsonxtojson.xls , but i am not able to see anuthing in output of this xslt.

    Thanks
    Well, I took the output of JSONRES.xslt and passed it to store://jsonx2json.xsl, and I got:

    curl --data-binary "@extractedContent.xml" http://192.168.144.200:14005
    { "Login":{ "Groups":{ "Group": } } }

    So I'm thinking you aren't piping the contexts around like you thing you are.

    The reason that you're not seeing anything in the probe is that the transform failed.

    Ken
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-29T00:49:54Z  
    • kenhygh
    • ‏2013-01-29T00:12:41Z
    Well, I took the output of JSONRES.xslt and passed it to store://jsonx2json.xsl, and I got:

    curl --data-binary "@extractedContent.xml" http://192.168.144.200:14005
    { "Login":{ "Groups":{ "Group": } } }

    So I'm thinking you aren't piping the contexts around like you thing you are.

    The reason that you're not seeing anything in the probe is that the transform failed.

    Ken
    Hi Ken,
    I defined both side as policy rule and input and ouput as PIPE for all the steps.Please explain more what do you mean by not piping correctly.
    Thanks
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-29T01:00:51Z  
    Hi Ken,
    I defined both side as policy rule and input and ouput as PIPE for all the steps.Please explain more what do you mean by not piping correctly.
    Thanks
    I am using soapui for testing did you get chance to try with soap ui. Should i use Curl only to test this.

    Pls Let me know should i need to set output type as binary for jsonx2josn.xslt transform action.
    Thanks
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-29T02:09:10Z  
    Hi Ken,
    I defined both side as policy rule and input and ouput as PIPE for all the steps.Please explain more what do you mean by not piping correctly.
    Thanks
    Not sure I understand what you have configured here.

    Can you export your firewall and attach it?

    k
  • SystemAdmin
    SystemAdmin
    6772 Posts

    Re: XML to JSON

    ‏2013-01-29T14:30:29Z  
    • kenhygh
    • ‏2013-01-29T02:09:10Z
    Not sure I understand what you have configured here.

    Can you export your firewall and attach it?

    k
    I am attaching xml export. I am able to see the output in prob when i defined the variable in ouput of jsonxtojson.xslt , but when i send request from soap ui i am getting below error

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <env:Fault>
    <faultcode>env:Client</faultcode>
    <faultstring>Internal Error</faultstring>
    </env:Fault>
    </env:Body>
    </env:Envelope>
  • kenhygh
    kenhygh
    1522 Posts

    Re: XML to JSON

    ‏2013-01-29T15:16:13Z  
    I am attaching xml export. I am able to see the output in prob when i defined the variable in ouput of jsonxtojson.xslt , but when i send request from soap ui i am getting below error

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <env:Fault>
    <faultcode>env:Client</faultcode>
    <faultstring>Internal Error</faultstring>
    </env:Fault>
    </env:Body>
    </env:Envelope>
    I don't think your rule can be both directions. Typically, when you have a client making a call in one format going to a provider in another format, you have a request rule to do the clientFormat->providerFormat, and a response rule doing providerFormat->clientFormat.

    If you turn on debug, there will be more details in the logs.

    Ken