667481 members! Sign up to stay informed.

Sponsored Links


Resources

Enterprise Java
Research Library

Get Java white papers, product information, case studies and webcasts

News News News Messages: 7 Messages: 7 Messages: 7 Printer friendly Printer friendly Printer friendly Post reply Post reply Post reply XML XML XML

Article: JSON for Ajax Web services

Posted by: Regina Lynch on October 19, 2006 DIGG
In this tip posted on SearchWebServices.com, Daniel Rubio digs into the advantages of JavaScript Object Notation (JSON), and how JSON as an approach has recently gained popularity with Ajax services and Web-based clients.

JSON is a data format, but one which is more naturally fit for browser data consumption. JSON is a subset to JavaScript, and by structuring a data payload as a JSON response, you are effectively bypassing the need to parse an XML document in a browser to get to the actual data. JSON uses a stripped-down syntax compliant with the native JavaScript interpreter provided on all browsers. Access and navigation to JSON data is done through the same standard JavaScript notation used to access string, array or hashtable values in a typical JavaScript application.
At first sight JSON and XML are similar, but it's only when you get down to accessing the internal data structures that you realize JSON is easier to deal with in a browser setting. Tapping values in JSON is as simple as navigating a JavaScript object due to its syntax, but doing so for an XML response entails a lengthier process, one of parsing and navigating an ad-hoc tree structure not particularly natural to a browser environment.

Truth be told, JSON dates back to 2002, so it's nothing out of the ordinary to structure data in a native JavaScript structure like an array or hashtable for later consumption by a browser. So why is it that JSON has recently gained popularity? The reason is another technology which has grown in recent times -- Ajax.
Rubio goes on to explain how JSON is an ideal solution to the increased demand that Ajax brings for processing data without asynchronous communication.
By using an alternate syntax which is natively related to a browser's JavaScript engine, JSON is an ideal combination for services bound to clients in the form of a browser.
What benefits have you been experiencing while using JSON? Do you agree that its recent boost in popularity has been due to Ajax?

Threaded replies

·  Article: JSON for Ajax Web services by Regina Lynch on Thu Oct 19 14:58:15 EDT 2006
  ·  AJAX Databinding w/ XML vs JSON by Charles Kendrick on Thu Oct 19 15:51:44 EDT 2006
  ·  Re: Article: JSON for Ajax Web services by Ilya Sterin on Thu Oct 19 18:01:54 EDT 2006
  ·  Re: Article: JSON for Ajax Web services by Alexa White on Fri Oct 20 00:45:45 EDT 2006
    ·  Re: Article: JSON for Ajax Web services by Ilya Sterin on Fri Oct 20 02:17:34 EDT 2006
  ·  Pointless... by Michael Connor on Fri Oct 20 15:10:54 EDT 2006
    ·  Re: Pointless... by Ilya Sterin on Fri Oct 20 16:00:34 EDT 2006
  ·  Need a worked out example to understand fully by venugopal darur on Wed Jul 08 11:57:42 EDT 2009
  Message #220657 Post reply Post reply Post reply Go to top Go to top Go to top

AJAX Databinding w/ XML vs JSON

Posted by: Charles Kendrick on October 19, 2006 in response to Message #220655
We've been using JSON as our AJAX payload since long before either term was coined, and yes, for in-browser manipulation it's definitely easier to work with for any data-related purpose.

Some people use an XML payload because they use XSLT to go to XHTML in the browser, and this has some apparent efficiencies to it until you realize that any customization of that XML -> XHTML pipeline has to be done in XSLT. Have you ever tried to write, eg, a date formatter in XSLT? It's essentially impossible, and so any such formatting has to be done as a separate JavaScript pass, throwing away all the apparent cleanliness and efficiency of using XSLT with an XML payload. I believe, although maybe Luke can correct me, that this issue is why TIBCO had to deprecate all their old grid APIs in their recent 3.2 release when they added Firefox support.

However, bigger picture, payload format is not that important when you have a client-side framework that supports high-level, SOA-style databinding. In SmartClient we use XPath-based binding to either JSON or XML payloads; the XPaths act on the JSON data in a manner similar to JXPath expressions on Java objects.

These two examples bind a grid and form to the Yahoo Image Search service, one in XML format and the other in JSON format. They differ in one property.

XML Binding
JSON Binding

These examples are mini-applications; if you want to see the same pattern in less code just look at the adjacent "JSON XPath Binding" example.

  Message #220660 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article: JSON for Ajax Web services

Posted by: Ilya Sterin on October 19, 2006 in response to Message #220655
I discussed some of this on one of my blog posts recently.

http://www.ilyasterin.com/enteprise_software/2006/09/ajaxxslt_views.html

Ilya

  Message #220666 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article: JSON for Ajax Web services

Posted by: Alexa White on October 20, 2006 in response to Message #220655
JSON does make sense to use if only a data transfers between client and server. Mostly, it is true for heavyweight clients. However, if we need to transfer not only data, but a layout the benefits using JSON over XML becomes not so obvious.

JSON presumes a predefined structure of transferred data. In case of XML, it might be an arbitrary content inserted directly to the browser DOM.
=
AlexaW, ajax-tutorials.com

  Message #220667 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article: JSON for Ajax Web services

Posted by: Ilya Sterin on October 20, 2006 in response to Message #220666
JSON does make sense to use if only a data transfers between client and server. Mostly, it is true for heavyweight clients. However, if we need to transfer not only data, but a layout the benefits using JSON over XML becomes not so obvious.

JSON presumes a predefined structure of transferred data. In case of XML, it might be an arbitrary content inserted directly to the browser DOM.
=
AlexaW, ajax-tutorials.com


Yeah, JSON is great for AJAX calls that require raw data, that needs to be consumed by JS for calculations or other manipulation. If the data is there only for presentation purposes, then XSLT and XML should be used. I think both (JSON and XML) can be used successfuly in an application.

Ilya

  Message #220694 Post reply Post reply Post reply Go to top Go to top Go to top

Pointless...

Posted by: Michael Connor on October 20, 2006 in response to Message #220655
I really don't see the point. XML is ubiquitous and there are tools aplenty. XPATH is supported by any respectable AJAX framework and makes it ridiculously simple to access XML elements. Tools such as Prototype (AJAX framework) make it easy to access such elements and return an array as opposed to having to iterate a NodeList.

I'm all for new tools but I'm having a hard time justifying retooling and the learning curve for my resources.

  Message #220697 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Pointless...

Posted by: Ilya Sterin on October 20, 2006 in response to Message #220694
I really don't see the point. XML is ubiquitous and there are tools aplenty. XPATH is supported by any respectable AJAX framework and makes it ridiculously simple to access XML elements. Tools such as Prototype (AJAX framework) make it easy to access such elements and return an array as opposed to having to iterate a NodeList.

I'm all for new tools but I'm having a hard time justifying retooling and the learning curve for my resources.


That's because you haven't done a highly concurrent app with a lot of heavy AJAX lifting. XML is great for presentation formats, as was explained, that require you to transform the data. When efficiency is a must, JSON provides a more efficient data format, which you can easily eval into js. Also, when you have mutliple async calls going on, say you have auto completion or something of that nature, when each event triggers an ajax call, i'd love to see how efficiently you can do that with xml. With JSON you minimize the data transfer, plus evaling into js structure and then traversing that is way more efficient than downloading a larger XML chunk and running an XPath expression against it.

Ilya

  Message #310795 Post reply Post reply Post reply Go to top Go to top Go to top

Need a worked out example to understand fully

Posted by: venugopal darur on July 08, 2009 in response to Message #220655
Hi,
I have worked on webservices and AJAX frame work,
I would like to request any one of you, to post a relevant example on JSON and Rest ful webservices, so I can understand fully.

Regards
venu

New content on TheServerSide.comNew content on TheServerSide.comNew content on TheServerSide.com

Dependency Injection in Java EE 6 - Part 1

Reza Rahman explores the features of the proposed JSR 299, Contexts and Dependency Injection for Java EE (CDI). When approved, it promises to be a key feature of Java EE 6. (November 2, Article)

SAML: It's Not just for Web services

SAML is an XML-based standard for exchanging authentication and authorization data between security domains. The single most important problem that SAML was created to solve is the Web browser Single Sign-On problem. Many organizations are debating whether to stay with version 1.1 or move to 2.0. This article makes observations about both options. (September 28, Article)

Programming is Also Teaching Your Team

Joe Ottinger takes a look at how people learn, and applies it to the practice of programming. He notes that understanding how people learn is an essential part of working in a programming team. (September 22, Article)

Can Java EE Deliver The Asynchronous Web?

Stephen Maryka gave us an article about the Asynchronous Web and posed a number of questions that get examined like an approach to delivering Asynchronous Web capabilities through extensions to existing Java EE technologies. (July 14, Article)

JSF Flex

JavaServer Faces Flex goal is to provide users capability in creating standard Flex components, part of flexSDK which is open sourced through MPL license, as normal JSF components. This article by Ji Hoon Kim will provide an overview of creating a simple multilingual JSF page consisting of JSF Flex tags. (June 29, Article)

The Rules of SOA - A Road to a Successful SOA Implementation

In this session Jeff explores the key characteristics of successful SOA projects. He covers some of the patterns, and anti-patterns, tool sets, and strategies that he himself learned the hard way. Last, he provides a strategy and blueprint for achieving a high likelihood of success in your SOA project. (June 23, Tech Talk)

Ari Zilka Talks About Terracotta 3.1

Ari Zilka, CTO of Terracotta, Inc., talks about the new features in Terracotta 3.1, announced during JavaOne and available now. (June 15, Tech Talk)

Enterprise Application Integration, and Spring

In this Tech Talk, Josh Long explores an integration challenge using Spring Integration and walks through the implementation, employing and expanding on the basic patterns of Enterprise Application Integration to tie together components into a function integration solution, and then demonstrates how Spring Integration helps address the integration requirements. (June 15, Tech Talk)

Google Web Toolkit: An Introduction

In this Tech Talk, David Geary teaches you: The basics of Google Web Toolkit; How to implement Ajax-enabled applications in Java; Internationalization; Hooking into the browser history mechanism; Remote procedure calls. (June 4, Tech Talk)

Just Enough Early Architecture to Guide Development

Jon Kern discusses the best architecture/technical solutions and ensure that they are repeated by all developers. By tackling the architecture up-front in a serial manner, subsequent parallel development will be much more manageable and predictable. (May 28, Tech Talk)

Productive Programmer: On the Lam from the Furniture Police

This keynote describes the frustrations of modern knowledge workers in their quest to actually get some work done, and solutions for how to guard yourself against all those distractions. Neal Ford talks about environments, coding, acceleration, automation, and avoiding repetition as ways to defeat the misguided attempts to sap your ability to produce good work. (May 26, Tech Talk)

Auto-Scaling Your Existing Web Application

Gil demonstrates how new, aggressive uses of already abundant compute capacity by common applications offer competitive value for application designers. (May 21, Tech Talk)

Automating Hibernate Mapping and Queries For Java Web Development

Chris Keene introduces WaveMaker as a new way to automate the ability to generate Hibernate classes in order to more quickly bring OR mapping into an application. (May 19, Article)

Auto-Scaling Your Existing Web Application

In this session Nati Shalom demonstrates how to take a standard Java EE web application and scale it out or down dynamically without changes to the application code. Seeing as most web applications are over-provisioned to meet infrequent peak loads, this is a dramatic change because it enables growing your application as needed, when needed, without paying for unutilized resources. (May 19, Tech Talk)

Free Book: Jakarta-Struts Live

Download the entire book of Jakarta-Struts Live and learn about Struts MVC, Tiles, the Validator, DynaActionForms, plug-ins, internationalization, and more.
(Book PDF Download)

Application Server Matrix

The Application Server Matrix is a detailed listing of J2EE vendors and their application server products, with information on latest version numbers, J2EE spec support and licensing, pricing, platform support, and links to product downloads and reviews.
(Application Server Comparison Matrix)

News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy
Site Map