Converted Json having Class name and id instead of the property values

42 views
Skip to first unread message

clr meno

unread,
Apr 13, 2017, 7:58:28 AM4/13/17
to Grails Dev Discuss
In my application, domain classes are added as grails plugin. The controller converts model object to JSON using : ResultObj as JSON
But the generated JSON has properties(that are objects themselves) as the class name and id instead of its property values. Any advise is much appreciated.


The object structure looks similar to the following:
 class Parent{
   String property1
    static hasMany = [property2:Child] 
    static constraints = {
   }
}


class Child{
   String property3
   static belongsTo = [property4:Parent]
   static constraints={}
}

Generated JSON looks like:
[{"class":"package.Parent","id":1,
  "property1":"value","property2":[{"class":"package.Child","id":1},{"class":"package.Child","id":2}]}]

clr meno

unread,
Apr 13, 2017, 8:02:24 AM4/13/17
to Grails Dev Discuss
My application uses Grails 2.4.4

Sergio Michels

unread,
Apr 14, 2017, 9:31:41 AM4/14/17
to grails-de...@googlegroups.com
Looking at the JSON you can see that your main object have properties and your relationship does not have. This is probably because of lazy loading:

The default fetch strategy used by Grails is "lazy", which means that the collection will be lazily initialized on first access. This can lead to the n+1 problem if you are not careful.

You can check out more about it in the docs: http://docs.grails.org/2.4.5/guide/GORM.html#fetching

So, modify your query to get your hasMany as eager, like this: http://stackoverflow.com/a/8652152

 

--
Sérgio Michels

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-discuss+unsub...@googlegroups.com.
To post to this group, send email to grails-dev-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/c29ad118-2166-45e3-8f0b-1a513cba8ab1%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

clr meno

unread,
Apr 14, 2017, 10:35:39 AM4/14/17
to Grails Dev Discuss
Thanks a lot Sergio. Configuring 'deep' in domain class solved the problem

--
Sérgio Michels

To post to this group, send email to grails-de...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages