Activate your free membership today | Log-in

Monday, February 6th, 2006

Transforming JSON

Category: JavaScript, Utility

<p>Stefan Gössner has seen a need to transform the JSON format, similarly how we transform XML via XSLT.

The implementation of JSON/T is by writing rules in JSON format itself.

Example

Given a JSON object such as:

JAVASCRIPT:
  1.  
  2. { "link": {"uri":"http://company.com", "title":"company homepage" }}
  3.  

and transform it to:

HTML:
  1.  
  2. <a href="http://company.com">company homepage</a>
  3.  

The JSON/T rule for doing this:

JAVASCRIPT:
  1.  
  2. { "link": "<a href="{link.uri}">{link.title}</a>" }
  3.  

You can download a JavaScript version of the JSON/T processor

Related Content:

Posted by Dion Almaer at 9:16 am
4 Comments

++++-
4 rating from 12 votes

4 Comments »

Comments feed TrackBack URI

It’s funny how the JSON/T rule looks different in my feed reader than on this page. In my feed reader it did not make sense, but here it does.

Comment by Hermann Klinke — February 6, 2006

In a similar vein, I wrote a json2dom script about a week ago. I don’t know if it has any practical application (probably not), but I wanted a way to create HTML elements from an Ajax response without using innerHTML, and this is what I came up with.

Comment by Randy Boland — February 6, 2006

[...] I first read about this over on Ajaxian and Randy Boland commented to say that he’s done a JSON to DOM script as well. [...]

Pingback by Migrating to JSON » Web 2.0 Blog — February 8, 2006

One major different between XSLT and a JSON transformer is that the later need to have a grammar for object/hash enumeration, I wonder whether JSONT support it, which I found lost in many js template engine.

Comment by yaojun85 — July 18, 2008

Leave a comment

You must be logged in to post a comment.