0

I have a requirement to convert xml to json. few elements required an array and for few elements it is not required.The Initial root tag also needs to be remove. Below is my xml.

 <?xml version="1.0" encoding="UTF-8" ?>
 <ns1:Account_Resp xmlns:ns1="test:test:services">
   <row>
    <bank>
        <bank>1234</bank>
        <bankShortName>customer</bankShortName>
    </bank>
    <card>
        <cardLastFour>000</cardLastFour>
        <cardType>00n</cardType>
    </card>
    <address>
        <city>CA</city>
        <country>CA</country>
    </address>
    <version>
        <symbol>20200702111359</symbol>
    </version>
    <locks>
        <lockType>06</lockType>
        <lockValidFrom>00</lockValidFrom>
        <lockValidTo>9</lockValidTo>
    </locks>
    <locks>
        <lockType>0</lockType>
        <lockValidFrom>000</lockValidFrom>
        <lockValidTo>99</lockValidTo>
    </locks>
    <infoStore>
        <informationCategoryCode></informationCategoryCode>
        <infoDateFrom></infoDateFrom>
                </infoStore>
    <changedTime>111359</changedTime>
    
    <standAloneFlag>false</standAloneFlag>
</row>
<row>
    <bank>
        <bank>1234</bank>
        <bankShortName>customer</bankShortName>
    </bank>
    <card>
        <cardLastFour>000</cardLastFour>
        <cardType>00n</cardType>
    </card>
    <address>
        <city>CA</city>
        <country>CA</country>
    </address>
    <version>
        <symbol>20200702111359</symbol>
    </version>
    <locks>
        <lockType>06</lockType>
        <lockValidFrom>00</lockValidFrom>
        <lockValidTo>9</lockValidTo>
    </locks>
    <locks>
        <lockType>0</lockType>
        <lockValidFrom>000</lockValidFrom>
        <lockValidTo>99</lockValidTo>
    </locks>
    <infoStore>
        <informationCategoryCode></informationCategoryCode>
        <infoDateFrom></infoDateFrom>
                </infoStore>
    <changedTime>111359</changedTime>
    
    <standAloneFlag>false</standAloneFlag>
</row>
   </ns1:Account_Resp>

For the above xml i have used the below XSLT code to convert into json.

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://use your namespace">
<xsl:output method="text"/>
<xsl:template match="/ns0:Account_Resp">{
    <xsl:apply-templates select="*"/> }
</xsl:template>

<!-- Object or Element Property-->
<xsl:template match="*">

    "<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
    

</xsl:template>


<!-- Array Element -->
<xsl:template match="*" mode="ArrayElement">
    <xsl:call-template name="Properties"/>
</xsl:template>


<!-- Object Properties -->
<xsl:template name="Properties">
    <xsl:variable name="childName" select="name(*[1])"/>
    <xsl:choose>
        <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
        <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
        <xsl:otherwise>{
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="*"/>
}</xsl:otherwise>
    </xsl:choose>
    <xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>


<!-- Attribute Property -->
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
</xsl:template>
</xsl:stylesheet>
 

I am expecting below output, but with above code it is not giving the correct output.

 [
        {
            "bank": {
                "bank": "1234",
                "bankShortName": "customer"
            },
            "card": {
                "cardLastFour": "000",
                "cardType": "00n"
            },
            "address": {
                "city": "Monicashire",
                "country": "CA"
            },
            "version": {
                "symbol": "2345"
            },
            "locks": [
                {
                    "lockType": "06",
                    "lockValidFrom": "00",
                    "lockValidTo": "9"
                },
                {
                    "lockType": "06",
                    "lockValidFrom": "000",
                    "lockValidTo": "999"
                }
            ],
            "infoStore": {
                "informationCategoryCode": "",
                "infoDateFrom": "",
                "infoDateTo": ""
            },
            "changedTime": "111359",
            
            "standAloneFlag": "false"
        },
        {
            "bank": {
                "bank": "1234",
                "bankShortName": "customer"
                
            },
            "card": {
                "cardLastFour": "000",
                "cardType": "00n",
                
            },
            "address": {
                "city": "ca",
                "country": "CA",
                
            },
            "version": {
                "symbol": "156"
            },
            "locks": [
                {
                    "lockType": "06",
                    "lockValidFrom": "00010101",
                    "lockValidTo": "99991231"
                },
                {
                    "lockType": "06",
                    "lockValidFrom": "00010101",
                    "lockValidTo": "99991231"
                }
            ],
            "infoStore":[ {
                "informationCategoryCode": "",
                "infoDateFrom": "",
                "infoDateTo": ""
            }
             ],
            "changedTime": "111359",
            
            "standAloneFlag": false
        }
    ]  

Please note that if the row is single also it should show the array symbol. locks and infostore nodes also should be always array even if it single in row.All fields should be in string and standAloneFlag field should be with out quotes.

Could you please help me what corrections i have do to for the correct output.

janardhan d
  • 35
  • 1
  • 6
  • The XSLT 3 and the XPath 3.1 spec have support for that, mapping the JSON data types to XDM 3.1 data types and defining a mapping JSON <--> XML. If you don't have XSLT 3 support then the usual approach is similar, define an intermediary XML format that can represent and distinguish the different JSON data types, then transform any input XML to that intermediary XML format for which you only need to implement one conversion to JSON. – Martin Honnen Jul 04 '20 at 07:02
  • Could you please help me with the code because i have issue with my above code.@MartinHonnen – janardhan d Jul 04 '20 at 08:22
  • Please ask only one SPECIFIC question at a time. As it is now, it looks like you are asking that someone takes a generic code that you have found somewhere and adapts it for you to your situation. – michael.hor257k Jul 04 '20 at 10:02
  • Hi michael, I am new to this XSLT code so i am trying with available codes. with the above code i am able to get some sort of output but not the desired output. For this reason only i have mentioned above code.@michael.hor257k – janardhan d Jul 04 '20 at 10:07
  • Dear janardhand, first of all, there's a typo in the beginning of your XML. A space is missing between the tag name and the namespace declaration (ns1:Account_Respxmlns:ns1). Second, i've worked with XSLT until version 2. Version 3 has JSON integration and you should look at it as @MartinHonnen said. Since you are a newbie, I suggest you to avoid () if your XML doesn't have to be generic. Define templates for each portion of the XML you desire to process. Declarative form is a bit harder than imperative form for beginners. – Carlos Bazilio Jul 04 '20 at 10:23

0 Answers0