Layer7 API Management

Handy XSLT to transform the jdbcXML result into JSON 

Jan 25, 2017 09:57 PM

Hi everyone.

 

I needed to generate a JSON from a bunch of rows coming from a JDBC query. This is how the jdbcQueryResult looks like (all fake data):

JDBC query result XML

<?xml version="1.0" encoding="UTF-8"?>
<L7j:jdbcQueryResult xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result">
  
<L7j:row>
     
<L7j:col name="latestread" type="java.lang.String">765.17</L7j:col>
     
<L7j:col name="country" type="java.lang.String">US</L7j:col>
     
<L7j:col name="address" type="java.lang.String">4918 Moland Crossing</L7j:col>
     
<L7j:col name="city" type="java.lang.String">Indianapolis</L7j:col>
     
<L7j:col name="name" type="java.lang.String">Shirley Reid</L7j:col>
     
<L7j:col name="id" type="java.lang.String">123456710</L7j:col>
     
<L7j:col name="state" type="java.lang.String">IN</L7j:col>
     
<L7j:col name="login" type="java.lang.String">sreid1</L7j:col>
     
<L7j:col name="status" type="java.lang.String">on</L7j:col>
  
</L7j:row>
  
<L7j:row>
     
<L7j:col name="latestread" type="java.lang.String">68.17</L7j:col>
     
<L7j:col name="country" type="java.lang.String">US</L7j:col>
     
<L7j:col name="address" type="java.lang.String">7 Merry Court</L7j:col>
     
<L7j:col name="city" type="java.lang.String">Midland</L7j:col>
     
<L7j:col name="name" type="java.lang.String">Timothy Kennedy</L7j:col>
     
<L7j:col name="id" type="java.lang.String">123456789</L7j:col>
     
<L7j:col name="state" type="java.lang.String">TX</L7j:col>
     
<L7j:col name="login" type="java.lang.String">tkennedy0</L7j:col>
     
<L7j:col name="status" type="java.lang.String">on</L7j:col>
  
</L7j:row>
</L7j:jdbcQueryResult>

 

This XSLT made the trick for me and I wanted to share as it might become handy for somebody else:

The mighty XSLT script

<xsl:stylesheet version="2.0" xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   
<xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
   
<xsl:strip-space elements="*"/>  
   
<xsl:template match="L7j:jdbcQueryResult">
        {
        "smartmeters": [
       
<xsl:for-each select="L7j:row">
            {
           
<xsl:for-each select="L7j:col">
                "
<xsl:value-of select = "@name"  />" : "<xsl:copy-of select="."/>"<xsl:choose><xsl:when test="position() != last()"><xsl:text>, </xsl:text></xsl:when></xsl:choose>
           
</xsl:for-each>
            }
<xsl:choose><xsl:when test="position() != last()"><xsl:text>, </xsl:text></xsl:when></xsl:choose>
       
</xsl:for-each>
        ]
        }
   
</xsl:template>  
</xsl:stylesheet>

 

This is the JSON after the transformation:

The expected JSON
{  "smartmeters": [              {                    "latestread" : "765.17",            "country" : "US",            "address" : "4918 Moland Crossing",            "city" : "Indianapolis",            "name" : "Shirley Reid",            "id" : "123456710",            "state" : "IN",            "login" : "sreid1",            "status" : "on" },          {                    "latestread" : "68.17",            "country" : "US",            "address" : "7 Merry Court",            "city" : "Midland",            "name" : "Timothy Kennedy",            "id" : "123456789",            "state" : "TX",            "login" : "tkennedy0",            "status" : "on" },          {                    "latestread" : "98.17",            "country" : "US",            "address" : "6554 Elka Park",            "city" : "Brooklyn",            "name" : "Katherine Rodriguez",            "id" : "1300000",            "state" : "NY",            "login" : "krodriguez2",            "status" : "on" },          {                    "latestread" : "NULL",            "country" : "US",            "address" : "214 Loomis Parkway",            "city" : "Pittsburgh",            "name" : "Margaret Watson",            "id" : "1300001",            "state" : "PA",            "login" : "mwatson3",            "status" : "on" },          {                    "latestread" : "NULL",            "country" : "US",            "address" : "650 6th Crossing",            "city" : "Riverside",            "name" : "Paul Ford",            "id" : "1300002",            "state" : "CA",            "login" : "pford4",            "status" : "off" },          {                    "latestread" : "NULL",            "country" : "US",            "address" : "4367 Waywood Alley",            "city" : "Tampa",            "name" : "Shirley Murray",            "id" : "1300003",            "state" : "FL",            "login" : "smurray5",            "status" : "on" },          {                    "latestread" : "NULL",            "country" : "US",            "address" : "70 Larry Way",            "city" : "Erie",            "name" : "Kimberly Mills",            "id" : "1300004",            "state" : "PA",            "login" : "kmills6",            "status" : "on" },          {                    "latestread" : "NULL",            "country" : "US",            "address" : "0 Clarendon Crossing",            "city" : "Shreveport",            "name" : "Thomas Baker",            "id" : "1300005",            "state" : "LA",            "login" : "tbaker7",            "status" : "on" },          {                    "latestread" : "43.17",            "country" : "US",            "address" : "7388 Del Sol Alley",            "city" : "Torrance",            "name" : "Michelle Elliott",            "id" : "1300006",            "state" : "CA",            "login" : "melliott8",            "status" : "on" },          {                    "latestread" : "NULL",            "country" : "US",            "address" : "8 Vermont Hill",            "city" : "Miami",            "name" : "Denise Wright",            "id" : "1300007",            "state" : "FL",            "login" : "dwright9",            "status" : "off" }  ] }

 

You can see the results for yourself here: https://gw02.apimca.com:8443/testJDBC  

Attached the policy and the XSLT.

 

Enjoy!

Statistics
0 Favorited
2 Views
2 Files
0 Shares
0 Downloads
Attachment(s)
zip file
jdbctoJSON.xsl.zip   555 B   1 version
Uploaded - May 29, 2019
zip file
jdbcResultXML-to-JSON.xml.zip   1 KB   1 version
Uploaded - May 29, 2019

Related Entries and Links

No Related Resource entered.