<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Intelligrape  Groovy &#38; Grails Blogs &#187; JSON</title>
	<atom:link href="http://www.intelligrape.com/blog/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.intelligrape.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 02 Feb 2012 07:48:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
			<title>Intelligrape  Groovy &amp; Grails Blogs</title>
			<url>http://www.intelligrape.com/blog/wp-content/uploads/2011/05/favicon2.ico</url>
			<link>http://www.intelligrape.com/blog</link>
			<width></width>
			<height></height>
			<description></description>
		</image>		<item>
		<title>Writing JSON APIs : Part II &#8211; Creating JSON Named Configs to Control What You Render</title>
		<link>http://www.intelligrape.com/blog/2011/12/29/writing-json-apis-part-ii-creating-json-named-configs-to-control-what-you-render/</link>
		<comments>http://www.intelligrape.com/blog/2011/12/29/writing-json-apis-part-ii-creating-json-named-configs-to-control-what-you-render/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 13:15:05 +0000</pubDate>
		<dc:creator>Vivek Krishna</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Grails Custom Marshaller]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[JSON API]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=4893</guid>
		<description><![CDATA[In the 1st part of the series, we looked at how to secure our application with Spring Security Basic Authentication and modifying the JSON Marshaller. However, it could often be the case that the same set of fields shouldn&#8217;t be returned on every JSON response.
For example, we could very well have a summary JSON for [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.intelligrape.com/blog/2011/11/16/writing-json-apis-part-i-creating-a-secure-rest-json-api-with-grails-and-spring-security-in-3-easy-steps/" target="_blank">1st part of the series</a>, we looked at how to secure our application with Spring Security Basic Authentication and modifying the JSON Marshaller. However, it could often be the case that the same set of fields shouldn&#8217;t be returned on every JSON response.</p>
<p>For example, we could very well have a summary JSON for Book, which just returns the id and name of the Book, ignoring the other details like ISBN, genre etc. A real life example would be a Product&#8217;s complete details when returning JSON for its show view, while returning just the id, name and price for its view in a shopping cart.</p>
<p>In addition to that, we could choose, not to override the default JSON Marshallers provided by Grails so that the standard rendering method is not tampered.</p>
<p>Grails provides an excellent solution called &#8220;Named Configurations&#8221; which comes to our aid here. That, combined with some minor modifications on our CustomDomainClassJSONMarshaller can help us achieve this.</p>
<p><strong>1. Add a static Map to the domain class, probably jsonProperties</strong> :</p>
<p>This property will hold the group name as key and the list of properties to be included as values.</p>
<pre class="brush: java;">

//Book.groovy

static jsonProperties = [summary:['name']] //summary is the group name
</pre>
<p><strong>2. Update the CustomDomainClassJSONMarshaller to have a property jsonPropertyGroup</strong> :</p>
<pre class="brush: java;">
String jsonPropertyGroup
public CustomDomainClassJSONMarshaller(boolean includeVersion, GrailsApplication application, String jsonPropertyGroup = &quot;&quot;) {
      this(includeVersion, new DefaultProxyHandler(), application, jsonPropertyGroup);
}
public CustomDomainClassJSONMarshaller(boolean includeVersion, ProxyHandler proxyHandler, GrailsApplication application, String jsonPropertyGroup) {
      this.includeVersion = includeVersion;
      this.proxyHandler = proxyHandler;
      this.application = application;
      this.jsonPropertyGroup = jsonPropertyGroup
}
</pre>
<p><strong>3. Update the custom marshaller registrations to create Named Configs :</strong></p>
<p>This method needs updation for two reasons</p>
<ul>
<li>Calling the newly created constructors</li>
<li>API changes in grails 2.0</li>
</ul>
<p>For creating a named config &#8220;summary&#8221;, we need to write something like</p>
<pre class="brush: java;">

JSON.createNamedConfig(JSONConstants.SUMMARY_JSON_MARSHALLER_GROUP){
     it.registerObjectMarshaller(new CustomDomainClassJSONMarshaller(false, grailsApplication, &quot;summary&quot;), 2)
}
</pre>
<p><strong>4. Using the newly registered named configs from within the controller:</strong></p>
<p>This is the final step. We can use the named config with a static method JSON.use(), which takes in the configuration&#8217;s name and a closure inside which we will call the <em>render &lt;XYZ&gt; as JSON</em> method</p>
<pre class="brush: java;">

JSON.use(&quot;summary&quot;){
     render Book.list() as JSON
}
</pre>
<p>I have updated the <a href="https://github.com/svivekkrishna/Json-API-Sample/tree/grails20NamedConfig" target="_blank">example application</a> to grails 2.0 and added an example to this approach.</p>
<p>Grails never ceases to amaze me. <img src='http://www.intelligrape.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2011/12/29/writing-json-apis-part-ii-creating-json-named-configs-to-control-what-you-render/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing JSON APIs : Part I &#8211; Creating a secure JSON API with Grails and Spring Security in 3 easy steps</title>
		<link>http://www.intelligrape.com/blog/2011/11/16/writing-json-apis-part-i-creating-a-secure-rest-json-api-with-grails-and-spring-security-in-3-easy-steps/</link>
		<comments>http://www.intelligrape.com/blog/2011/11/16/writing-json-apis-part-i-creating-a-secure-rest-json-api-with-grails-and-spring-security-in-3-easy-steps/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 06:01:57 +0000</pubDate>
		<dc:creator>Vivek Krishna</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[REST Calls]]></category>
		<category><![CDATA[spring security]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=4502</guid>
		<description><![CDATA[We had a requirement in a recent project to expose some of the functionality we had via a JSON API. The functionality needed to be secure, as was the initial web interface which exposed the functionality. We were using Spring Security for the security aspect of our application.
The spring security plugin, together with a secured [...]]]></description>
			<content:encoded><![CDATA[<p>We had a requirement in a recent project to expose some of the functionality we had via a JSON API. The functionality needed to be secure, as was the initial web interface which exposed the functionality. We were using Spring Security for the security aspect of our application.</p>
<p>The spring security plugin, together with a secured controller and a custom JSON marshaller(which overrides the default functionality of render as JSON method) gave us a very simple, yet elegant and powerful JSON API which was secure. Here are the three steps that we followed</p>
<p><strong>1. Setting up Spring Security Plugin: </strong></p>
<p>The first step is to set up Spring Security Plugin to expose our JSON based controllers to use Basic Authentication, instead of the standard web based authentication. This is done by adding the lines given below in Config.groovy</p>
<pre class="brush: java;">
//Enable Basic Auth Filter
grails.plugins.springsecurity.useBasicAuth = true
grails.plugins.springsecurity.basic.realmName = &quot;JSON API Example&quot;
//Exclude normal controllers from basic auth filter. Just the JSON API is included
grails.plugins.springsecurity.filterChain.chainMap = [
'/json/**': 'JOINED_FILTERS,-exceptionTranslationFilter',
'/**': 'JOINED_FILTERS,-basicAuthenticationFilter,-basicExceptionTranslationFilter'
]
</pre>
<p>More details about the authentication mechanism can be found <a href="http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/guide/9%20Authentication.html" target="_blank">here</a>.<br />
<strong>2. Adding a Controller with required actions:</strong></p>
<p>Naturally, this is the next step. We added a controller which would expose the functionalities required(another reason why most of our logic should be in our services instead of controllers). A sample controller would look like this.</p>
<pre class="brush: java;">
package jsonapi
import grails.plugins.springsecurity.Secured
import grails.converters.JSON
import com.intelligrape.example.json.Book

@Secured([&quot;ROLE_USER&quot;])
class JsonController {
      def getBooks = {
          render Book.list() as JSON
      }
}
</pre>
<p><strong>3. Customizing the Marshaller to change the way some properties like enums are rendered:</strong></p>
<p>We had to change the way some of the properties like enums were going to be rendered. We just had to render the property name and the id of the enum. So instead of a json map like</p>
<pre class="brush: java;">

&quot;genre&quot;:{&quot;enumType&quot;:&quot;com.intelligrape.example.json.Book$Genre&quot;,&quot;name&quot;:&quot;FICTION&quot;}
</pre>
<p>we needed</p>
<pre class="brush: java;">

&quot;genre&quot;:&quot;FICTION&quot;
</pre>
<p>I sought help from David Bower&#8217;s <a href="http://manbuildswebsite.com/2010/02/15/rendering-json-in-grails-part-3-customise-your-json-with-object-marshallers/" target="_blank">post</a> and created my own custom Domain Class Marshaller for JSON with a modification to just use the Enum value if the property happened to be an enum. This is a very powerful feature because it allows us to customize the way in which we want to render the values when creating a JSON or XML document from our classes. We can even customize it to have an <em>excludes </em>list in our domain class where we can specify the properties to be excluded while constructing our JSON or XML<em> </em></p>
<p>With this, we had a JSON API ready in very little time. I have extracted the functionality into a small example application, which has been shared on <a href="https://github.com/svivekkrishna/Json-API-Sample" target="_blank">Github</a>.</p>
<p>Yet another example of how simple grails has made it easy for developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2011/11/16/writing-json-apis-part-i-creating-a-secure-rest-json-api-with-grails-and-spring-security-in-3-easy-steps/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JQuery : create URL query string from JSON/Array</title>
		<link>http://www.intelligrape.com/blog/2011/05/04/jquery-create-url-query-string-from-jsonarray/</link>
		<comments>http://www.intelligrape.com/blog/2011/05/04/jquery-create-url-query-string-from-jsonarray/#comments</comments>
		<pubDate>Wed, 04 May 2011 06:46:45 +0000</pubDate>
		<dc:creator>Amit Jain</dc:creator>
				<category><![CDATA[Javascript/Ajax/JQuery]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[querystring]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=3761</guid>
		<description><![CDATA[Hi Friends,

I was writing javascript to reload my page with different parameters. So I needed to create the queryString of those parameters but I didn&#8217;t want to create it of my own. I already knew about jQuery.serialize() which serializes the form and creates the queryString, but unfortunately I didn&#8217;t have form. So that gave me [...]]]></description>
			<content:encoded><![CDATA[<p>Hi Friends,<br />
<span id="more-3761"></span><br />
I was writing javascript to reload my page with different parameters. So I needed to create the queryString of those parameters but I didn&#8217;t want to create it of my own. I already knew about <a href="http://api.jquery.com/serialize/">jQuery.serialize()</a> which serializes the form and creates the queryString, but unfortunately I didn&#8217;t have form. So that gave me opportunity to learn about new <a href="http://api.jquery.com/jQuery.param/">jQuery.param()</a> which converts the JSON/Array into query string. Let us look at few examples below :</p>
<pre class="brush: jscript;">
var myParams = {country:&quot;US&quot;, customerId:&quot;1&quot;}
jQuery.param(myParams);

Output: &quot;country=US&amp;customerId=1&quot;
</pre>
<pre class="brush: jscript;">
var myParams = {name: &quot;Amit Jain&quot;,days:['Mon','Tue','Sat'] };
jQuery.param(myParams);

Output: &quot;name=Amit+Jain&amp;days%5B%5D=Mon&amp;days%5B%5D=Tue&amp;days%5B%5D=Sat&quot;
</pre>
<pre class="brush: jscript;">
var myParams = {name: &quot;Diana&quot;,address:{
  line1:&quot;232 Alder Dr.&quot;,
  line2:&quot;Alder City&quot;,
  state : &quot;Utah&quot;,
  country: &quot;US&quot;
}};
jQuery.param(myParams);

Output : &quot;name=Diana&amp;address%5Bline1%5D=232+Alder+Dr.&amp;address%5Bline2%5D=Alder+City&amp;address%5Bstate%5D=Utah&amp;address%5Bcountry%5D=US&quot;
</pre>
<p>Hope this helped!<br />
<!--more--><br />
Cheers!</p>
<p>~~Amit Jain~~<br />
amit@intelligrape.com</p>
<p>http://www.intelligrape.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2011/05/04/jquery-create-url-query-string-from-jsonarray/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JQuery: Send JSON Objects with an ajax request</title>
		<link>http://www.intelligrape.com/blog/2010/06/11/jquery-send-json-object-with-an-ajax-request/</link>
		<comments>http://www.intelligrape.com/blog/2010/06/11/jquery-send-json-object-with-an-ajax-request/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 10:30:09 +0000</pubDate>
		<dc:creator>Amit Jain</dc:creator>
				<category><![CDATA[Javascript/Ajax/JQuery]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=1039</guid>
		<description><![CDATA[Hi Friends,
Lets discuss today about sending JSON objects with ajax request using JQuery. We have number of functions in jQuery to kick-off an ajax request. But for sending JSON objects along with the request, I chose jQuer.ajax(). It takes various parameters url, type, data, dataType, beforeSend etc. Its API can be found here.
Lets look at [...]]]></description>
			<content:encoded><![CDATA[<p>Hi Friends,</p>
<p>Lets discuss today about sending JSON objects with ajax request using JQuery. We have number of functions in jQuery to kick-off an ajax request. But for sending JSON objects along with the request, I chose jQuer.ajax(). It takes various parameters url, type, data, dataType, beforeSend etc. Its API can be found <a href="http://api.jquery.com/jQuery.ajax/">here</a>.</p>
<p>Lets look at example given below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">jQuery.<span style="color: #006600;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
          url<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>Url of the action<span style="color: #339933;">&gt;,</span>
          type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
          data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;amit&quot;</span><span style="color: #339933;">,</span> id<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;json&quot;</span><span style="color: #339933;">,</span>
          beforeSend<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;&amp;</span> x.<span style="color: #006600;">overrideMimeType</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              x.<span style="color: #006600;">overrideMimeType</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;application/j-son;charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 	     <span style="color: #006600; font-style: italic;">//Write your code here</span>
          <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above example works for simple JSON object. Now lets see how we can send JSON objects list as given below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> jsonObjects <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;amit&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;ankit&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;atin&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;puneet&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
jQuery.<span style="color: #006600;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
          url<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>Url of the action<span style="color: #339933;">&gt;,</span>
          type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
          data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>students<span style="color: #339933;">:</span> JSON.<span style="color: #006600;">stringify</span><span style="color: #009900;">&#40;</span>jsonObjects<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;json&quot;</span><span style="color: #339933;">,</span>
          beforeSend<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;&amp;</span> x.<span style="color: #006600;">overrideMimeType</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              x.<span style="color: #006600;">overrideMimeType</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;application/j-son;charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 	     <span style="color: #006600; font-style: italic;">//Write your code here</span>
          <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you notice, for sending my json objects it has not been written directly as data: jsonObjects. As it expects the JSON object passed to it written as key value pair. So we made students the key. And since we have json objects stored in a variable, we need to expand the json objects list using stringify(), otherwise it would be sent as a java script object.</p>
<p>Now on the server we can parse the JSON object, and use it as the list of objects of type map. For example</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #808080; font-style: italic;">//this code is written in grails </span>
<span style="color: #a1a100;">import grails.converters.JSON;</span>
List<span style="color: #66cc66;">&lt;</span>JSON<span style="color: #66cc66;">&gt;</span> students <span style="color: #66cc66;">=</span> JSON.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">students</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">//students in request params is parsed to json objects and stored in the List</span>
<span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;Student id: &quot;</span> + students<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">studentId</span>    <span style="color: #808080; font-style: italic;">//first element of the students list is accessed as a map holding a key studentId</span></pre></div></div>

<p>Hope this helpled!</p>
<p>~~Amit Jain~~<br />
amit@intelligrape.com</p>
<p>http://www.IntelliGrape.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/06/11/jquery-send-json-object-with-an-ajax-request/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Create JSON object Using Grails converter (Only selective fields from lists of objects)</title>
		<link>http://www.intelligrape.com/blog/2010/05/13/create-json-object-using-grails-converter-only-selective-fields-from-lists-of-objects/</link>
		<comments>http://www.intelligrape.com/blog/2010/05/13/create-json-object-using-grails-converter-only-selective-fields-from-lists-of-objects/#comments</comments>
		<pubDate>Thu, 13 May 2010 12:33:02 +0000</pubDate>
		<dc:creator>Salil</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Javascript/Ajax/JQuery]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[json converter]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=681</guid>
		<description><![CDATA[This post might help you if you want to get JSON (JavaScript Object Notation) Object on browser. Grails framework provides you very efficient way to achieve this.
For this you need to import grails JSON convertor in your code.


import grails.converters.JSON


Below is the code snapshot which converts java based lists of Objects to JSON object


HashMap jsonMap = [...]]]></description>
			<content:encoded><![CDATA[<p>This post might help you if you want to get JSON (JavaScript Object Notation) Object on browser. Grails framework provides you very efficient way to achieve this.</p>
<p>For this you need to import grails JSON convertor in your code.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #a1a100;">import grails.converters.JSON</span></pre></div></div>

</blockquote>
<p>Below is the code snapshot which converts java based lists of Objects to JSON object</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #aaaadd; font-weight: bold;">HashMap</span> jsonMap <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">HashMap</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
List<span style="color: #66cc66;">&lt;</span>Company<span style="color: #66cc66;">&gt;</span> companyList <span style="color: #66cc66;">=</span> Company.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
List<span style="color: #66cc66;">&lt;</span>Contact<span style="color: #66cc66;">&gt;</span> employeeList <span style="color: #66cc66;">=</span> Employee.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
jsonMap.<span style="color: #006600;">companies</span> <span style="color: #66cc66;">=</span> companyList.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span>comp -<span style="color: #66cc66;">&gt;</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span>id: comp.<span style="color: #006600;">id</span>, name: comp.<span style="color: #006600;">name</span>, address: comp.<span style="color: #006600;">address</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
jsonMap.<span style="color: #006600;">employees</span> <span style="color: #66cc66;">=</span> employeeList.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span>emp -<span style="color: #66cc66;">&gt;</span>
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span>id: emp.<span style="color: #006600;">id</span>, name: emp.<span style="color: #006600;">name</span>, companyId: emp.<span style="color: #006600;">companyId</span>, role: emp.<span style="color: #006600;">role</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
render jsonMap <span style="color: #000000; font-weight: bold;">as</span> JSON</pre></div></div>

</blockquote>
<p>So you got it &#8211; <strong>MAGIC lies in &#8220;render jsonMap as JSON&#8221; statement.</strong></p>
<p><em><strong>Output sent to Browser</strong></em>:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;companies&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
       <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">281</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot; Company Name Incorporated&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;address&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;street-address, zone-address, city, state, country, zip12&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
       <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">282</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot; Other company LLC&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;address&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;street-address1, zone-address2, city, state, country, zip34&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;employees&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
       <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">123</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Employee123 Name&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;companyId&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">281</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;role&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Designer&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
       <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">127</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Employee127 Name&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;companyId&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">281</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;role&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Supervisor&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
       <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">129</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Employee129 Name&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;companyId&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">282</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;role&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Inspector&quot;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</blockquote>
<p>Isn&#8217;t it cool <img src='http://www.intelligrape.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . How to use JSON Objects on browser is out of scope of this post. I will try to write another post soon &#8211; how to query JSON based data to produce client-side results effectively (example &#8211; client side search).</p>
<p>Cheers!!<br />
Salil Kalia</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/05/13/create-json-object-using-grails-converter-only-selective-fields-from-lists-of-objects/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to use $.getJSON() method of jQuery with grails?</title>
		<link>http://www.intelligrape.com/blog/2009/03/02/how-to-use-getjson-method-of-jquery-with-grails/</link>
		<comments>http://www.intelligrape.com/blog/2009/03/02/how-to-use-getjson-method-of-jquery-with-grails/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 12:39:39 +0000</pubDate>
		<dc:creator>Chandan Luthra</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Javascript/Ajax/JQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=27</guid>
		<description><![CDATA[How to use $.getJSON() method of jQuery with grails?
Retriving a JSON string from Grails is very easy. You have to just write the following in your controller&#8217;s action
Let me explain you with an example of populating a HTML table using JSON response:
In this example, we have table with columns &#8211; name,address and gender. We want [...]]]></description>
			<content:encoded><![CDATA[<p>How to use $.getJSON() method of jQuery with grails?</p>
<p>Retriving a JSON string from Grails is very easy. You have to just write the following in your controller&#8217;s action</p>
<p>Let me explain you with an example of populating a HTML table using JSON response:<br />
In this example, we have table with columns &#8211; name,address and gender. We want the table to be populated without doing a full page refresh, using an Ajax call.</p>
<p>Domain Class:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">class</span> MyDomain <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> name
    <span style="color: #aaaadd; font-weight: bold;">String</span> address
    <span style="color: #aaaadd; font-weight: bold;">String</span> gender
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Controller Class :</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">class</span> MyController <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">def</span> someaction <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #aaaadd; font-weight: bold;">List</span> myDomains <span style="color: #66cc66;">=</span> MyDomain.<span style="color: #006600;">findAllByGender</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">gender</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&lt;</span>em<span style="color: #66cc66;">&gt;</span><span style="color: #808080; font-style: italic;">/*Let say 5 objects are retrieved*/</span><span style="color: #66cc66;">&lt;</span>/em<span style="color: #66cc66;">&gt;</span>
      render myDomains <span style="color: #000000; font-weight: bold;">as</span> JSON
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The above code in action will render a<strong> JSON</strong> string in the following format:</p>
<p><code>[<br />
{<br />
"name"		: "John",<br />
"address"	: "New York",<br />
"gender"	: "Male"<br />
},<br />
{<br />
"name"		: "Rob",<br />
"address"	: "Indonasia",<br />
"gender"	: "Male"<br />
},<br />
{<br />
"name"		: "Shayam",<br />
"address"	: "New Delhi",<br />
"gender"	: "Male"<br />
},<br />
{<br />
"name"		: "Chang",<br />
"address"	: "Thailand",<br />
"gender"	: "Male"<br />
},<br />
{<br />
"name"		: "Ali",<br />
"address"	: "London",<br />
"gender"	: "Male"<br />
}<br />
]</code></p>
<p>following script would be writtent on the client Side (in the GSP):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
$.<span style="color: #006600;">document</span>.<span style="color: #006600;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#someid'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>$.<span style="color: #006600;">getJSON</span><span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;${createLink(controller:'my',action:'someaction')}&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>gender<span style="color: #339933;">:</span><span style="color: #3366CC;">'Male'</span><span style="color: #339933;">,</span> ajax<span style="color: #339933;">:</span> <span style="color: #3366CC;">'true'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>myDomains<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> myHTMLString <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> myDomains.<span style="color: #006600;">length</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
myHTMLString <span style="color: #339933;">=</span> myHTMLString <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td&gt;'</span> <span style="color: #339933;">+</span> myDomains<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/td&gt;'</span>
myHTMLString <span style="color: #339933;">=</span> myHTMLString <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td&gt;'</span> <span style="color: #339933;">+</span> myDomains<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">address</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/td&gt;'</span>
myHTMLString <span style="color: #339933;">=</span> myHTMLString <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td&gt;'</span> <span style="color: #339933;">+</span> myDomains<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006600;">gender</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/td&gt;&lt;/tr&gt;'</span>
<span style="color: #009900;">&#125;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table#mytable'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #009900;">&#40;</span>myHTMLString<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The HTML code would be like this :</p>
<p>&lt;div id=&#8221;someid&#8221;&gt;<br />
Click for JSON Response<br />
&lt;/div&gt;<br />
&lt;table id=&#8221;mytable&#8221;&gt;<br />
&lt;!&#8211;empty table &#8211;&gt;<br />
&lt;/table&gt;</p>
<p>When you click on the displayed text the html page would become like following:</p>
<p><code>&lt;div id="someid"&gt;<br />
Click for JSON Response<br />
&lt;/div&gt;<br />
&lt;table id="mytable"&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;John&lt;/td&gt;<br />
&lt;td&gt;New York&lt;/td&gt;<br />
&lt;td&gt;Male&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Rob&lt;/td&gt;<br />
&lt;td&gt;Indonasia&lt;/td&gt;<br />
&lt;td&gt;Male&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Shayam&lt;/td&gt;<br />
&lt;td&gt;New Delhi&lt;/td&gt;<br />
&lt;td&gt;Male&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Chang&lt;/td&gt;<br />
&lt;td&gt;Thailand&lt;/td&gt;<br />
&lt;td&gt;Male&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;Ali&lt;/td&gt;<br />
&lt;td&gt;London&lt;/td&gt;<br />
&lt;td&gt;Male&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;</code></p>
<p>any kind of suggestions and comments are welcome&#8230;&#8230;</p>
<p>Regards,<br />
Chandan Luthra</p>
<p>http://www.IntelliGrape.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2009/03/02/how-to-use-getjson-method-of-jquery-with-grails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

