Quantcast

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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

clr meno
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}]}]

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/e9ed69a0-d336-4f45-98a5-4578d0222bdd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

clr meno
My application uses Grails 2.4.4

On Thursday, April 13, 2017 at 7:58:28 AM UTC-4, clr meno wrote:
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}]}]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

sergiomichels
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

On Thu, Apr 13, 2017 at 9:02 AM, clr meno <[hidden email]> wrote:
My application uses Grails 2.4.4

On Thursday, April 13, 2017 at 7:58:28 AM UTC-4, clr meno wrote:
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}]}]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAHTf1NWCa20tLDVVJ9JeBqmUbFpE3GAwKgb9vTLPfH4XZApKig%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

clr meno
Thanks a lot Sergio. Configuring 'deep' in domain class solved the problem

On Friday, April 14, 2017 at 9:31:41 AM UTC-4, Sergio Michels wrote:
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: <a href="http://docs.grails.org/2.4.5/guide/GORM.html#fetching" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fdocs.grails.org%2F2.4.5%2Fguide%2FGORM.html%23fetching\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF6AeUPPGCq1_2wJmzrHIJnshcBFQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fdocs.grails.org%2F2.4.5%2Fguide%2FGORM.html%23fetching\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF6AeUPPGCq1_2wJmzrHIJnshcBFQ&#39;;return true;">http://docs.grails.org/2.4.5/guide/GORM.html#fetching

So, modify your query to get your hasMany as eager, like this: <a href="http://stackoverflow.com/a/8652152" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fa%2F8652152\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHE6gBabjVBeN1GHKwsY4yLTxtgDQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fa%2F8652152\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHE6gBabjVBeN1GHKwsY4yLTxtgDQ&#39;;return true;">http://stackoverflow.com/a/8652152

 

--
Sérgio Michels

On Thu, Apr 13, 2017 at 9:02 AM, clr meno <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="29LKzuuSDwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">men...@...> wrote:
My application uses Grails 2.4.4

On Thursday, April 13, 2017 at 7:58:28 AM UTC-4, clr meno wrote:
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}]}]

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="29LKzuuSDwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">grails-dev-discuss+unsubscribe@....
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="29LKzuuSDwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">grails-de...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/grails-dev-discuss/c29ad118-2166-45e3-8f0b-1a513cba8ab1%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/grails-dev-discuss/c29ad118-2166-45e3-8f0b-1a513cba8ab1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/grails-dev-discuss/c29ad118-2166-45e3-8f0b-1a513cba8ab1%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/grails-dev-discuss/c29ad118-2166-45e3-8f0b-1a513cba8ab1%40googlegroups.com.

For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/debb0754-604d-4c54-ac7d-2c095cbd3cf8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...