Create JSON object Using Grails converter (Only selective fields from lists of objects) « Intelligrape Groovy & Grails Blogs

Create JSON object Using Grails converter (Only selective fields from lists of objects)

Posted by Salil

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 = new HashMap()
List<Company> companyList = Company.list()
List<Contact> employeeList = Employee.list()
 
jsonMap.companies = companyList.collect {comp ->
return [id: comp.id, name: comp.name, address: comp.address]
}
 
jsonMap.employees = employeeList.collect {emp ->
return [id: emp.id, name: emp.name, companyId: emp.companyId, role: emp.role]
}
 
render jsonMap as JSON

So you got it – MAGIC lies in “render jsonMap as JSON” statement.

Output sent to Browser:

{
  "companies": [
       {"id":281,"name":" Company Name Incorporated", "address": "street-address, zone-address, city, state, country, zip12"},
       {"id":282,"name":" Other company LLC", "address": "street-address1, zone-address2, city, state, country, zip34"},
  ],
  "employees": [
       {"id":123,"name":"Employee123 Name","companyId":281, "role":"Designer"},
       {"id":127,"name":"Employee127 Name","companyId":281, "role":"Supervisor"},
       {"id":129,"name":"Employee129 Name","companyId":282, "role":"Inspector"}
  ]
}

Isn’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 – how to query JSON based data to produce client-side results effectively (example – client side search).

Cheers!!
Salil Kalia

  • Share/Bookmark
This entry was posted on May 13th, 2010 at 6:03 pm and is filed under Grails, Javascript/Ajax/JQuery . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “Create JSON object Using Grails converter (Only selective fields from lists of objects)”

  1. lukasz says:

    You can do that a little bit simpler:

    jsonMap.companies = companyList.collect{it.properties['id','name']}

  2. pdf software says:

    I’ve learn several excellent stuff here. Certainly price bookmarking for revisiting. I surprise how much effort you place to make one of these wonderful informative website.

  3. Wild Planet Spy Gear Expert Mission Case…

    [...]Create JSON object Using Grails converter (Only selective fields from lists of objects) « Intelligrape Groovy & Grails Blogs[...]…

  4. Chris Love says:

    Is this more or less efficient than using registerObjectMarshaller ??

  5. Latest|Free Download|Info|Android|iPhone|Mac|iPad|…

    [...]Create JSON object Using Grails converter (Only selective fields from lists of objects) « Intelligrape Groovy & Grails Blogs[...]…

  6. Vishal sahu says:

    Thanks Salil for sharing this,
    It helped me a lot…!!!

Leave a Reply