Transforming JSON

Update: (Think that) finally managed to get from Windows Live Spaces the formatting I wanted…

Update: Enhanced the JSON Lexer to properly deal with escaped characters within strings. How to handle \b and \f ???  

====================================

Ever wanted to access and manipulate JSON as ordinary XML? To transform it with XSLT?

No problem, use the f:json-document() and f:json-file-document() as provided by FXSL.

Here is a quick example:

Let’s have (yes, this is the Yahoo Traffic Service):

 

 

<xsl:variable name="vStr">

 {"ResultSet":  

   {"LastUpdateDate":"1178683597",

    "Result":[{"type":"construction",

               "Title":"Construction work, on I-5 NB at SENECA ST",

               "Description":"I 5 N Construction work, Left Lane Blocked on I 5 northbound from Seneca Street to Pine Street starting 11:00 PM, 05 08 07 for several days from 11:00pm to 05:00am on Tuesdays, Wednesdays and Thursdays . From milepost 165 to milepost 167",

               "Latitude":"47.614353",

               "Longitude":"-122.329586",

               "Direction":"NB",

               "Severity" :  2,

               "ReportDate":1178604000,

               "UpdateDate":1178608792,

               "EndDate":1178712000},

 

              {"type":"construction",

               "Title":"Construction work, on I-5 NB at UNIVERSITY ST",

               "Description":"I 5 N Construction work, On ramp Blocked on I 5 northbound at University Street starting 10:00 PM, 05 08 07 until further notice from 10:00pm to 05:00am on Tuesdays, Wednesdays and Thursdays . From milepost 165 to milepost 166",

               "Latitude":"47.615975",

               "Longitude":"-122.328988",

               "Direction":"NB",

               "Severity" : 2,

               "ReportDate":1178600400,

               "UpdateDate":1178608793,

               "EndDate":1178712000},

 

              {"type":"incident",

               "Title":"Lane closed, on WA-99 at 4TH AVE",

               "Description":"SR 99 Road construction, right lane closed on SR 99 in both directions from 4TH AVE W to 7TH AVE SE starting 8:00 PM, 05 07 07 for a week from 08:00pm to 06:00am on Mondays, Tuesdays, Wednesdays and Thursdays . From milepost 50 to milepost 51",

              "Latitude":"47.634877",

              "Longitude":"-122.344338",

              "Direction":"N\/A",

              "Severity":2,

              "ReportDate":1178506800,

              "UpdateDate":1178608788,

              "EndDate":1178715600}

            ]

   }

}

</xsl:variable>

 

 

Then,  f:json-document($vStr) evaluates to:

 

<ResultSet>  

   <LastUpdateDate>1178683597</LastUpdateDate>

   <Result>

      <type>construction</type>

      <Title>Construction work, on I-5 NB at SENECA ST</Title>

      <Description>I 5 N Construction work, Left Lane Blocked on I 5 northbound from Seneca Street to Pine Street starting 11:00 PM, 05 08 07 for several days from 11:00pm to 05:00am on Tuesdays, Wednesdays and Thursdays . From milepost 165 to milepost 167</Description>

      <Latitude>47.614353</Latitude>

      <Longitude>-122.329586</Longitude>

      <Direction>NB</Direction>

      <Severity>2</Severity>

      <ReportDate>1178604000</ReportDate>

      <UpdateDate>1178608792</UpdateDate>

      <EndDate>1178712000</EndDate>

   </Result>

   <Result>

      <type>construction</type>

      <Title>Construction work, on I-5 NB at UNIVERSITY ST</Title>

      <Description>I 5 N Construction work, On ramp Blocked on I 5 northbound at University Street starting 10:00 PM, 05 08 07 until further notice from 10:00pm to 05:00am on Tuesdays, Wednesdays and Thursdays . From milepost 165 to milepost 166</Description>

      <Latitude>47.615975</Latitude>

      <Longitude>-122.328988</Longitude>

      <Direction>NB</Direction>

      <Severity>2</Severity>

      <ReportDate>1178600400</ReportDate>

      <UpdateDate>1178608793</UpdateDate>

      <EndDate>1178712000</EndDate>

   </Result>

   <Result>

      <type>incident</type>

      <Title>Lane closed, on WA-99 at 4TH AVE</Title>

      <Description>SR 99 Road construction, right lane closed on SR 99 in both directions from 4TH AVE W to 7TH AVE SE starting 8:00 PM, 05 07 07 for a week from 08:00pm to 06:00am on Mondays, Tuesdays, Wednesdays and Thursdays . From milepost 50 to milepost 51</Description>

      <Latitude>47.634877</Latitude>

      <Longitude>-122.344338</Longitude>

      <Direction>N/A</Direction>

      <Severity>2</Severity>

      <ReportDate>1178506800</ReportDate>

      <UpdateDate>1178608788</UpdateDate>

      <EndDate>1178715600</EndDate>

   </Result>

</ResultSet>

 

 and you can transform nicely this XML document with XSLT now.

The functions f:json-document() and f:json-file-document() are available immediately from the CVS of the FXSL project.

All this pure XSLT magic (and sure, expect more to come) is possible with using the LR Parsing Framework implemented in FXSL.

More to come  soon.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s