Wednesday, February 28th, 2007

Open Source JSON Tag Library Released

Category: JavaScript, JSON, Library

<p>James Wiltshire and his team at ATG have created and released a JSP tag library that is used to render JSON data from within JSP code. They use JSON as our data format of choice for
all AJAX applications being written at ATG, and they use this library
extensively to render the JSON.

This allows JSON data rendering JSPs to sit alongside existing HTML
rendering JSPs as part of the view layer when using any of the various
Java MVC frameworks (Struts, WebWork, Spring MVC, et al).

The libray has been released under an Apache License v2.0 on SourceForge.


< %@ taglib prefix="json" uri="" %>




< view plain text >
  1. {
  2.   "aString": "foo",
  3.   "anotherString": "Foo",
  4.   "bool1": true,
  5.   "numeric1": 3,
  6.   "anObject": {
  7.     "fee": "fi",
  8.     "fo": "fum"
  9.   },
  10.   "anArray": ["Mon","Tue","Wed","Thur","Fri","Sat","Sun"]
  11. }

Related Content:

Posted by Dion Almaer at 7:20 am

3.5 rating from 43 votes


Comments feed TrackBack URI

Why ? What is there to gain from using such JSP tag library? First, there could potentially lots and lots of XML to write. Just look at the example in the two boxes above. Then there’s the issue of no separation between markup and content. It’s a lot easier to manipulate JSON data in a Struts action rather than with XML in this JSP tag library.

Comment by Kevin Hoang Le — February 28, 2007

In the context of an MVC application, if you see the JSON data as an alternative view of your model, then it may make sense to define the representation of that data within a JSP as opposed to within your controller code.

With a Java-based webapp the majority of your view layer will be rendering HTML from within JSPs. The usual control flow with a struts-based app would be:
1. Submit request to server from browser.
2. Request is handled by appropriate action class which invokes any necessary business logic.
3. Action forwards/redirects control to appropriate view component – usually a JSP.
4. View renders response to browser.

With a non-AJAX application, the view would usually consist of a JSP rendering HTML. If you wanted to add AJAX functionality to such an app using progressive enhancement, then you could add an alternate JSON-rendering view layer as an alternate JSP using the JSON taglib. The action can then forward to the HTML view when a normal (non AJAX) request is handled, and forward to the JSON view when an AJAX request is handled with minimal changes to your controller or business logic.

Another advantage to using a JSP to render your JSON data is that if you have complex model objects it is very convenient to use the JSTL format tags to assist with formating your data. Take a shopping cart within an ecommerce application for example – you will want to render monetary values in a localized form, and you may need to include other localized content, such as localized product information, within your cart data. Using the JSTL format tags is a very convenient way to achieve this and is likely how your developers will have already implemented it for your html view.

There may well be times when manipulating JSON data within your controller code (struts action class if using struts) will work fine for you. If so, then fantastic. This taglib just offers an alternative approach that may or may not suit your particular needs if you’re developing Java-based web applications.

Comment by James Wiltshire — February 28, 2007

IMHO the command class passed to view layer would suit better. Transforming Java Object to JSON notation is two line of code. You don’t have to write jsp that 3 times larger than produced JSON.

Comment by Robert — February 28, 2007

Excellent entry for The Daily WTF.


Comment by Valery Silaev — March 1, 2007

I think you should explain above example more clearly for everybody know how to using json tag library for handle ajax function and display on jsp page.
Thanks very much

Comment by Hungnm — March 2, 2007

If serializing your model objects directly to JSON works for your application, then that is a great architecture to use. I have used that approach in many AJAX applications in the past. I have found however that using this approach does depend on having a model where the objects lend themselves to serialization easily. If you have a complex model layer, or you need to send an aggregation or a subset of data from the available model components as JSON, then it can be easier to specify this in the JSP layer.

Additionally, if you need to retrofit an existing webapp with AJAX features, using the approach I describe requires minimal changes to the existing server architecture. All that you need to add is a new view layer (the JSON-rendering JSPs), and a simple switch in the controller that redirects to the JSON layer for AJAX requests. If working on a greenfield project, being written with AJAX functionality in mind from day one, then serializing your model as you describe may well be a better option as you can design that in from day one.

I’m not advocating any architectural approach over another. If you find that rendering JSON data from the JSP layer is an approach that works for your particular application, then the JSON tag library will assist you in doing that. If you try to render JSON directly from a JSP without using a taglib such as this, then you will very quickly run into problems with whitespace, Javascript escaping, unmatched commas, braces, etc ruining the syntax of the rendered JSON.

I’ve added an examples section to the json-taglib project website. It shows a real-world example of how the taglib could be used in the context of an e-commerce shopping cart. I hope this helps you.

Comment by James Wiltshire — March 4, 2007

Hello all,
I would like to know how much javascript these tags could replace.

I make an example:

I have a button in my jsp that invokes an Ajax.Submit function and a callback function that has the role of scrolling a JSONArray in order to render the elements inside a table (that’s inside a div).
This JSONArray is provided by a struts action:

How could I pass this JSONArray from the callback function to the json tags? (if it’s possible?)

Is there any example over the web of a similar situation?

thx for any advise

How could I replace my javascript with these tags?

Comment by lele — April 11, 2007

Leave a comment

You must be logged in to post a comment.