{"id":681,"date":"2010-05-13T18:03:02","date_gmt":"2010-05-13T12:33:02","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=681"},"modified":"2010-06-09T15:24:08","modified_gmt":"2010-06-09T09:54:08","slug":"create-json-object-using-grails-converter-only-selective-fields-from-lists-of-objects","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/create-json-object-using-grails-converter-only-selective-fields-from-lists-of-objects\/","title":{"rendered":"Create JSON object Using Grails converter (Only selective fields from lists of objects)"},"content":{"rendered":"<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>\n<p>For this you need to import grails JSON convertor in your code.<\/p>\n<blockquote>\n<pre lang=\"groovy\">import grails.converters.JSON<\/pre>\n<\/blockquote>\n<p>Below is the code snapshot which converts java based lists of Objects to JSON object<\/p>\n<blockquote>\n<pre lang=\"groovy\">HashMap jsonMap = new HashMap()\r\nList<Company> companyList = Company.list()\r\nList<Contact> employeeList = Employee.list()\r\n\r\njsonMap.companies = companyList.collect {comp ->\r\nreturn [id: comp.id, name: comp.name, address: comp.address]\r\n}\r\n\r\njsonMap.employees = employeeList.collect {emp ->\r\nreturn [id: emp.id, name: emp.name, companyId: emp.companyId, role: emp.role]\r\n}\r\n\r\nrender jsonMap as JSON\r\n<\/pre>\n<\/blockquote>\n<p>So you got it &#8211; <strong>MAGIC lies in &#8220;render jsonMap as JSON&#8221; statement.<\/strong><\/p>\n<p><em><strong>Output sent to Browser<\/strong><\/em>:<\/p>\n<blockquote>\n<pre lang=\"javascript\">\r\n{\r\n  \"companies\": [\r\n       {\"id\":281,\"name\":\" Company Name Incorporated\", \"address\": \"street-address, zone-address, city, state, country, zip12\"},\r\n       {\"id\":282,\"name\":\" Other company LLC\", \"address\": \"street-address1, zone-address2, city, state, country, zip34\"},\r\n  ],\r\n  \"employees\": [\r\n       {\"id\":123,\"name\":\"Employee123 Name\",\"companyId\":281, \"role\":\"Designer\"},\r\n       {\"id\":127,\"name\":\"Employee127 Name\",\"companyId\":281, \"role\":\"Supervisor\"},\r\n       {\"id\":129,\"name\":\"Employee129 Name\",\"companyId\":282, \"role\":\"Inspector\"}\r\n  ]\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>Isn&#8217;t it cool :-). 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>\n<p>Cheers!!<br \/>\nSalil Kalia<\/p>\n","protected":false},"excerpt":{"rendered":"<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. 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 [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":26},"categories":[7],"tags":[4840,9,49,233],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/681"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=681"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/681\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}